Laravel 4 and uploadifile not working always getting 500 (Internal Server Error)

263 views Asked by At

I have my route:

Route::get('/file', array(
    'as' => 'files',
    'uses' => 'fileController@getFileUpload'
));

Route::post('/uploadfile', array(
    'as' => 'uploadfile',
    'uses' => 'fileController@postfileupload'
));

Now i have uploadifive setup on my /file route and sending post request to /uploadfile

code for uploadfile is here:

    $file = Input::file('file_upload'); 
    $destinationPath = 'files/CDN/'.str_random(8);
    echo $filename = $file->getClientOriginalName();
    $extension =$file->getClientOriginalExtension();  
    $uploadSuccess = Input::file('file')->move($destinationPath, $filename);

but always i am getting 500 (Internal Server Error)

I checked my directory CHMOD is 0777 and i am linking to right route as when i remove above code from /uploadfile and place

echo 200;

it returns success.

I have tried adding blade form tag also but uploadifive actually does not depends upon form element at all. It post with AJAX.

1

There are 1 answers

0
Lonare On

I finally got the work around when you see the var_dump of the response from a normal file posting form via blade Laravel approach you will find the Uploadfile object is created with the name of file (specified in the form field) but on the other hand when you send the same request via Uploadifive you will find that it sends an array which include a node called [FileData] which holds an object for that file so basically you need to assign that object pointer to the $file variable like this and it will work perfectly:

$data               = Input::all(); 
$file               = $data['Filedata']; 
$destinationPath    = public_path().'files/'.str_random(8);
$filename           = $file->getClientOriginalName();
$extension          = $file->getClientOriginalExtension();  
$uploadSuccess      = $file->move($destinationPath, $filename);
if( $uploadSuccess ) {
   return Response::json('success', 200);
} else {
   return Response::json('error', 400);
}

Works like charm :)