EDIT:
I have managed to solve the first issues with wrong file type. By changing the if statement to:
if ($v->fails())
{
return Response::json('error', 400);
}
I was able to return the error for the any non image file type. However, I still get the below problem for anything greater than my size limit.
I have the following code that uploads a photo to my server using Dropzone.js and laravel 4.
public function upload()
{
$file = Input::file('file');
$input = Input::all();
$rules = array
(
'file' => 'mimes:jpeg,bmp,png,gif | Max:2048'
);
$v = Validator::make($input, $rules);
if( $v->passes() )
{
$l = 0;
$dir = 'photos';
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) !== false){
if (!in_array($file, array('.', '..')) && !is_dir($dir.$file))
$l++;
}
}
$dir = 'photos/' . $l;
$i = 0;
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) !== false){
if (!in_array($file, array('.', '..')) && !is_dir($dir.$file))
$i++;
}
}
if($i >= 10000)
{
$l++;
$myDir = 'photos/' . $l;
mkdir($myDir);
$destinationPath = $myDir;
$i = 0;
}
else
{
$destinationPath = 'photos/' . $l;
}
public_path().'/uploads'
$filename = $i + 1 . ".jpg";
$upload_success = Input::file('file')->move($destinationPath, $filename);
if( $upload_success )
{
return 10;
}
else
{
return 20;
}
}
else
{
return Redirect::to('upload')->withErrors($v);
}
}
Now the code works fine, it successfully uploads the photos as intended, however I can't seem to get it to show errors correctly.
As shown in my code, I have two validation rules one for size and one for file type. Each one of these have their own issues.
If I upload an incorrect file type I get no error back. If I upload too large of a file I do get an error, but it seems to be an error to do with the laravel config as opposed to my validation rules:
{"error":{"type":"Symfony\\Component\\HttpFoundation\\File\\Exception\\FileException","message":"The file \"Window quotes LP.psd\" exceeds your upload_max_filesize ini directive (limit is 2048 kb).","file":"C:\\wamp\\www\eal_estate\\vendor\\symfony\\http-foundation\\Symfony\\Component\\HttpFoundation\\File\\UploadedFile.php","line":255}}
So my question is how do I get these errors to work with the validation? I don't want to display that long error message to end user, so how do I both fix it so my validation errors work, as well as not allow that default error to come through?