Getting 500 Error when wrong file type is uploaded
Created 7 years ago by emergingdznsI'm using the file field type in my module and have the extension set for pdf, doc, docx, zip on the folder where it will upload the file when submitted.
if (request()->file('upload')) {
if ($upload = $uploader->upload(request()->file('upload'), $folder)) {
$fileAtt = $upload->getAttributes();
$upload->will = [$fileAtt['id']];
$upload->file_id = $fileAtt['id'];
}
}
But when the user submits with a file of another extension in the logs, it throws a 500 error saying "local.ERROR: Exception: The file must be a file of type: pdf, doc, docx, zip. in /path/to/site/core/anomaly/files-module/src/File/FileUploader.php:98"
Shouldn't the error not be a major error but rather return back a friendly error?
Thanks @piterden but I don't that's the problem. In my method container I'm calling out "FileUploader $uploader" which is loading use Anomaly\FilesModule\File\FileUploader;
Unless the FileUploader doesn't use the UploadedFile class, I'm not sure that this is the issue. If I use a correct file type it uploads just fine.
I have a code like:
if ($image = $images->where('image_id', $product['image_id'])->first())
{
$error = trans('anomaly.module.files::error.generic');
$file = $imagesPath.$image['image_path'];
$uploaded = new UploadedFile(
$file,
array_get($image, 'image_name'),
array_get($image, 'mime_type'),
filesize($file),
null,
true
);
$file = $this->uploader->upload($uploaded, $folder);
}
Not tested it with wrong types, cause it is from seeder.
@ryanthompson I'm still having a problem with the uploading doing a hard crash exception when the user attempts to upload the wrong file type. I have a field type that is set to allow only PNG, JPG, JPEG but a user tried to upload a .doc file. It gave a 500 error. Is there a way to prevent such a harsh crash and instead have a redirect to back with an error message?
@emergingdzns since you're handling it yourself you need to validate it / respond accordingly.
Uploaded file MUST be an instance of
UploadedFile
class.