8.4+: File upload error: file too large but it's not

Permalink
I'm uploading a 200 kB file with this:
$service = $this->app->make('helper/security');
if (is_uploaded_file($photo)) {
    $importer = new Importer();
    $photo = $_FILES['photo']['tmp_name'];
    $photo_name = $service->sanitizeString(mb_strtolower($_FILES['photo']['name']));
    $file = $importer->import($photo, $photo_name);
    if ($file instanceof Version) {
        ...
    }
    else {
        $e->add(Importer::getErrorMessage($file)); // <- ERROR HERE
    }
}
else {
    $e->add($_FILES['photo']['error']);

it throws an error: "Uploaded file is too large. The current value of upload_max_filesize is 2M". But the file is only 200 kB! Why does that throw the exception?

BTW, the photo does get uploaded into the File Manager. What's wrong with the file being an instance of the Version?

linuxoid
 
linuxoid replied on at Permalink Reply
linuxoid
I got it! My Version was Concrete\Core\File\Version but it must be Concrete\Core\Entity\File\Version

The Documentation pagehttps://documentation.concrete5.org/developers/working-with-files-an... in the section Handling Permissions has the error:
if ($result instanceof \Concrete\Core\File\Version) {
mnakalay replied on at Permalink Reply
mnakalay
Hey @linuxoid. Just as an FYI, if this is something for the PRB, $_FILES will not fly. You should get it from the active request using $request->files the same way you do $request->request for $_POST and $request->query for $_GET
linuxoid replied on at Permalink Reply
linuxoid
Hi! Yes, it is for PRB. Thank you very much for reminding me.
linuxoid replied on at Permalink Reply
linuxoid
Can't find the request info. What would be an equivalent of $_FILES['photo']['name']? $this->request->files['photo']['name']? Or is the file itself in the $this->request->files->get('photo')? How do I get the rest of file info, e.g. name etc.?
mnakalay replied on at Permalink Best Answer Reply
mnakalay
Yes you use
$this->request->files->get('photo')

What you get back is either an array if several file upload inputs with the same name exist or a vendor\symfony\http-foundation\File\UploadedFile object. If it's an array, it is an array of UploadedFile objects

Check the function handleUpload() in the file concrete\controllers\backend\file.php to see how they grab the information. It will also show you how to get the path to the file and other things.

Anyway, once you have that UploadedFile object you have functions to get the file name, original file name, extension, mime type from the object
linuxoid replied on at Permalink Reply
linuxoid
There's nothing in that file about getting the file name, type, size. I only see they get paths, that's it. I've checked the Symphony's UploadedFile, I guess it's the getClientOriginalName etc.

BTW I don't understand why I can't use the $_FILES if it's already. What's the reason?
mnakalay replied on at Permalink Reply
mnakalay
Sprry I wasn't clearer. The file file.php is just an example to see how to grab the object and also its path. For the file information such as name, extension... They are all from the class vendor\symfony\http-foundation\File\UploadedFile which is what you get back.

So in file.php look to see how they deal with multiple or single file and stuff like that. And then for each uploaded file you get back a vendor\symfony\http-foundation\File\UploadedFile object so look inside that class to see the functions available to you
linuxoid replied on at Permalink Reply
linuxoid
$photo = $_FILES['photo']['tmp_name'];
$photo_name = mb_strtolower($_FILES['photo']['name']);
$photo_size = $_FILES['photo']['size'];
$photo_type = pathinfo($photo_name, PATHINFO_EXTENSION);

equivalent:
$files = $this->request->files;
$photo = $files->get('photo');
$photo_name = mb_strtolower($photo->getClientOriginalName());
$photo_size = $photo->getClientSize();
$photo_type = $photo->getClientOriginalExtension();
mnakalay replied on at Permalink Reply
mnakalay
yep, looks good.