Welcome Guest, Not a member yet? Register   Sign In
File upload verification
#1

(This post was last modified: 02-26-2020, 04:15 AM by GotExx. Edit Reason: unwanted spaces )

Hi all,

I'm trying to set up a check for uploading a file.

CI keeps returning the error: Archive file is not a valid uploaded file.
Also, I am not sure that CI recognizes gzip files as application/x-gzip

The PHP function mime_content_type returns application/x-gzip to me.

My controller function
PHP Code:
public function uploadBackup()
{
    $validation =  \Config\Services::validation();
    $validation->setRule('archiveFile''Archive file''uploaded[archiveFile]|ext_in[archiveFile,gzip]|mime_in[archiveFile,application/x-gzip]');
    if ($this->request->getMethod() !== 'post' || !$validation->withRequest($this->request)->run())
    {
        var_dump($validation->listErrors()); die;
    }
    else
    {
        var_dump("ok"); die;
    }


My HTML
Code:
<?= form_open(route_to('uploadBackup'), 'class="form-inline"'); ?>
    <div class="col-10">
        <div class="custom-file">
            <input type="file" class="custom-file-input" id="archiveFile" name="archiveFile">
            <label class="custom-file-label" for="customFile">Uploader une archive</label>
        </div>
    </div>
    <div class="col-2">
        <button type="submit" class="btn btn-primary">Submit</button>
    </div>
<?= form_close(); ?>

Thank's !
Reply
#2

Hi GotExx,

I run your code and it works fine for me.

If you get error message saying "Archive file is not a valid uploaded file." it means uploaded[archiveFile] validation fails. It has nothing to do with MIME types, as then it would tell you "Archive file does not have a valid mime type". 

You can check it yourself if you first remove uploaded[archiveFile] validation from your code and run it - it should pass OK. Then change mime_in[archiveFile,application/x-gzip] into mime_in[archiveFile,application/anything] and you will see "Archive file does not have a valid mime type". error message.

So you have to analyze why uploaded[archiveFile] validation fails. This validation rule calls isValid() function that will fail if anything from the following list occurs (as per official docs, https://codeigniter4.github.io/userguide...ht=isvalid):
  • The file exceeds your upload_max_filesize ini directive.
  • The file exceeds the upload limit defined in your form.
  • The file was only partially uploaded.
  • No file was uploaded.
  • The file could not be written on disk.
  • File could not be uploaded: missing temporary directory.
  • File upload was stopped by a PHP extension.
Many default settings limit max filesize to 10MB, and as you are dealing with archive file you might upload really large files. Try to upload smaller file, probably it will work. Othrwise check if your webserver allows you to upload files at all.
Reply
#3

Hi zahhar,

Thank's for your reply.

Ok I've increase the upload_max_filesize in php.ini to 1G but when i remove the first rule uploaded[archiveFile], i simply get the second error : Archive file does not have a valid file extension by ext_in[archiveFile,gzip] rule.

When i also remove the mime_in[archiveFile,application/x-gzip] rule i get the third error: Archive file does not have a valid mime type.

I've also try to remove all the rules and only put this:
PHP Code:
$validation->setRule('archiveFile''Archive file''is_image[archiveFile]'); 
and use a PNG image in my form, I get Archive file is not a valid, uploaded image file. As if the webserver can't correctly define the mime type..

For information this is my configuration:
  • PHP 7.3.1
  • Red Hat 7
  • CI4-rc3
Thank's again !
Reply
#4

(This post was last modified: 02-26-2020, 08:05 AM by zahhar.)

That is what I said: you have an issue with accessing uploaded file from CI.
Your code is fine, and there is no issue in CI4-rc3 (however, you might want to upgrade to stable version).

I would suggest you to contact your hosting provider if you have a virtual managed hosting, or if you are self-managed, then address one by one each and every item from the list.

Most probably one of those 2 prevents you:
[ ] The file could not be written on disk.
[ ] File could not be uploaded: missing temporary directory.

Also,
- what yoyu are using in between of PHP and CI - Apache? Nginx? Anything else?
- do you run PHP in safe_mode, as FCGI, or?
Reply
#5

(02-26-2020, 08:04 AM)zahhar Wrote: That is what I said: you have an issue with accessing uploaded file from CI.
Your code is fine, and there is no issue in CI4-rc3 (however, you might want to upgrade to stable version).

I would suggest you to contact your hosting provider if you have a virtual managed hosting, or if you are self-managed, then address one by one each and every item from the list.

Most probably one of those 2 prevents you:
[ ] The file could not be written on disk.
[ ] File could not be uploaded: missing temporary directory.

Also,
- what yoyu are using in between of PHP and CI - Apache? Nginx? Anything else?
- do you run PHP in safe_mode, as FCGI, or?
Reply
#6

Please Read:

CodeIgniter 4 User Guide - Working With the File
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

I ran into the same problem, getting the "not a valid file" error. After checking folder permissions, max upload size, etc, I tested file upload to the target folder using a stand-alone script with the native PHP function and it worked. Next, I inserted that code in the Controller in place of the CI upload sample code and it worked. I still haven't been able to determine why the CI4 code fails.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB