This is just a guess, but it may be that a temporary file can't be serialized because it expires at the end of the request. I would ASSUME that the error would be thrown when you try to store it in the session, but it look like it may be happening when you try to use it instead. Instead of storing the file right in the session, using the UploadedFile class, see if you can do something like copy it to the temp directory and then save a new File class to the session instead.
Aug 22, 2014
10
Level 2
Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed
I'm not sure what this error is for since I have been using this throughout my entire site and it works fine in those areas. This is my model:
<?php namespace Duplicolor\Models\User;
use \Illuminate\Auth\UserTrait;
use \Illuminate\Auth\UserInterface;
use \Illuminate\Auth\Reminders\RemindableTrait;
use \Illuminate\Auth\Reminders\RemindableInterface;
use \Input;
use \Auth;
use \Config;
use \Intellectproductions\Verify\Verify;
use \Zizaco\Entrust\HasRole;
use \Laracasts\Presenter\PresentableTrait;
use \Duplicolor\Traits\ApprovalTrait;
use \Duplicolor\Traits\BaseTrait;
use \Duplicolor\Helpers\Uploader;
use \Session;
class User extends Verify implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait, HasRole, PresentableTrait, ApprovalTrait, BaseTrait;
public static $rules = array(
'username' => 'sometimes|required|max:50|unique:users,username',
'email' => 'sometimes|required|email|max:50|unique:users,email',
'password' => 'sometimes|required|confirmed',
'password_confirmation' => 'sometimes|required',
'first_name' => 'sometimes|required|max:45',
'last_name' => 'sometimes|required|max:45',
'street' => 'max:100',
'city' => 'sometimes|required|max:45',
'state' => 'max:2',
'zip' => 'max:7',
'country' => 'max:50',
'rules' => 'sometimes|required|accepted',
'show_name' => 'boolean',
'avatar' => 'image',
'banner' => 'image'
);
protected $fillable = array('first_name', 'last_name', 'street', 'city', 'state', 'zip', 'country', 'rules', 'signup', 'show_name');
protected $presenter = '\Duplicolor\Presenters\User\UserPresenter';
protected $identify = array('email', 'username'); // identify column in the db
protected static $directory; // User avatar directory
protected static $bannerDirectory; // User banner directory
protected static $avatarWidth = 124;
protected static $avatarHeight = 124;
protected static $bannerWidth = 1024;
protected static $bannerHeight = 338;
function __construct($attributes = array()) {
parent::__construct($attributes);
$this->purgeFilters[] = function($key) {
$purge = array('rules', 'signup');
return ! in_array($key, $purge);
};
$this->approvalColumn = 'confirmed';
self::$directory = Config::get('app.url') . Config::get('app.userPath');
self::$bannerDirectory = Config::get('app.url') . Config::get('app.userBannerPath');
}
public static function boot()
{
parent::boot();
User::saving(function($user)
{
// Check to see if they are changing their avatar
if( ! is_null(Session::get('avatar')) )
{
// Upload image and get filename
$uploader = new Uploader();
$uploader->setDirectory(static::$directory);
if( ! $uploader->uploadImage( Session::get('avatar'), array(static::$avatarWidth, static::$avatarHeight) ) )
{
return false;
}
$this->avatar = $uploader->getRecentFile();
}
// Check to see if they are changing their banner
if( ! is_null(Session::get('banner')) )
{
// Upload image and get filename
$uploader = new Uploader();
$uploader->setDirectory(static::$bannerDirectory);
if( ! $uploader->uploadImage( Session::get('banner'), array(static::$bannerWidth, static::$bannerHeight) ) )
{
return false;
}
$this->banner = $uploader->getRecentFile();
}
});
}
}
My Uploader class looks like this:
<?php namespace Duplicolor\Helpers;
/**
* Uploader Class
*
* All methods related to handling files and using the Resizer object
*
* @author Haley Schillig <http://intellectproductions.com/>
* @version 1.0
*
*/
use \Auth;
use \File;
use \Imager;
class Uploader {
protected $directory; // Directory for images
protected $recentFileUpload; // Recent file upload
protected $width; // Width of image if resized
protected $height; // Height of image if resized
public function setDirectory( $value )
{
$this->directory = $value;
}
public function getRecentFile()
{
return $this->recentFileUpload;
}
/**
* Upload images
* @param image Image object
* @param resizeArgs Width and height of image
*
*/
public function uploadImage( $image, array $resizeArgs = array() )
{
$fileName = $this->generateFileName( $image );
$image->move($this->directory, $fileName);
// now resize and upload
if(count($resizeArgs) > 0)
{
// Set width and height
$this->width = $resizeArgs[0];
$this->height = $resizeArgs[1];
// Resize image
$this->resize($this->directory . $fileName);
}
$this->recentFileUpload = $fileName;
return true;
}
}
I didn't show the entire class because the error is specifically being thrown at this line:
$image->move($this->directory, $fileName);
I did a dd on the $image and it is returning the image object when the form is set to files => true so that's coming back fine. $this->directory holds the correct directory and $fileName holds the right value that was expected.
This is how I'm executing it all:
/**
* Edit banner of user
*
*/
public function postEditBanner()
{
$user = $this->user->getById( Auth::id() );
Session::put('banner', Input::file('graphic'));
if( ! $user->updateUniques() )
{
return Redirect::to(URL::previous())
->withInput(Input::except('graphic'))
->withErrors( $user->errors()->all() );
}
return Redirect::to(URL::previous())
->withInput(Input::except('graphic'))
->with('success', 'Your banner was changed successfully');
}
Thanks if anyone can see anything blaringly wrong..
Please or to participate in this conversation.