Question

I have a php upload file process that works quite well. However I wanted to add some validation to ensure large files weren't uploaded, however when I was testing I noticed some strange behaviour;

I set post_max_size & upload_max_filesize to 5M (restarted server) but found that if I uploaded a file larger than 5M the POST data was lost, so I couldn't do a check on $_FILES['uploadedFile']['size']

I kinda solved the issue by ensuring that the post_max_size & upload_max_filesize were set to 10M (assuming of course no one will be silly enough to try a file bigger than that - its really for text files so they shouldn't) After which I could do my size validation without issue;

if($_FILES['uploadedFile']['size'] < 3072000)
{                   
    //do upload stuff                   
}
else
{                   
    $errorMessage .= 'File size to big, exceeds 3M Limit.';
}

I assume the problem is that the POST is getting obliterated because it exceeded post_max_size??? I searched through a lot of pages but couldn't see anyone else having a problem with this so I'm not sure if my assumption is right. If I upload a file bigger than 10Mm I still get the problem.

Was it helpful?

Solution

You are correct. Uploaded data is ignored, when the size limit is exceeded. So, you don't get access to the ['size'] info.

Instead, you need to validate your upload like this:

<?php

if ($_FILES['uploadedFile']['error'] == UPLOAD_ERR_OK)
{
    // success - move uploaded file and process stuff here
}
else
{
    // error
    switch ($_FILES['uploadedFile']['error'])
    {
        case UPLOAD_ERR_INI_SIZE:
            echo "The uploaded file exceeds the upload_max_filesize directive in php.ini."; break;

        case UPLOAD_ERR_FORM_SIZE:
            echo "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form."; break;

        case UPLOAD_ERR_PARTIAL:
            echo "The uploaded file was only partially uploaded. "; break;

        // etc... see the full list here http://uk1.php.net/manual/en/features.file-upload.errors.php
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top