views:

1705

answers:

5

Hi, I have a file (image) upload script in PHP that I use to upload and resize images... It uses a simple MIME type and size validation so only jpg images are allowed and 1MB max file size.

I recently discovered a problem. When I try tu upload a .avi file using the script, the script processes the file like its the correct MIME type and size and then just do nothing, just takes me back to the upload form without any error message. (Instead of showing a "file too big" message).

I mean, if I try to upload a .gif or .txt or something else I get an error, as expected. If I try to upload any file bigger than 1MB I get an error, as expected. Only when I try to upload a .avi file with more than 1MB I dont get any kind of error..... Well, here the first par of the code:

// define a constant for the maximum upload size
define ('MAX_FILE_SIZE', 1024000);

if (array_key_exists('upload', $_POST)) {
// define constant for upload folder
define('UPLOAD_DIR', 'C:/Wamp/www/Version-1.4/posters_uploaded/');

// replace any spaces in original filename with underscores. At the same time, assign to a simpler variable
$file = str_replace(' ', '_', $_FILES['image']['name']);

// convert the maximum size to KB
$max = number_format(MAX_FILE_SIZE/1024, 1).'kb';
// create an array of permitted MIME types
$permitted = array('image/jpeg','image/pjpeg');
// begin by assuming the file is unacceptable
$sizeOK = false;
$typeOK = false;

// check that file is within the permitted size
if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
 $sizeOK = true;
}
// check that file is of a permitted MIME type
foreach ($permitted as $type) {
 if ($type == $_FILES['image']['type']) {
  $typeOK = true;
 break;
 }
}

if ($sizeOK && $typeOK) {
 switch($_FILES['image']['error']) {
  case 0: // ...................

I'm just modifying a build PHP code so Im no expert... Any suggestions?? Thanks.

+2  A: 

http://us3.php.net/manual/en/features.file-upload.common-pitfalls.php

It looks like your upload_max_filesize ini-setting is too low. This would cause no error to be displayed when you upload a very large file such as an AVI video.

The reason you're seeing the errors with text files and .jpg images is likely because the size of those files are greater than 1 MB, but below your upload_max_filesize setting in php.ini.

Try echoing the value of ini_get("max_upload_filesize") and see what the value is if you don't have access to the php.ini file directly.

John Rasch
Checked the php.ini... I have 2MB limit... ;Maximum allowed size for uploaded files.upload_max_filesize = 2MSo, thats not the problem....
Jonathan
The maximum size is 2 GB, is that what you meant?
John Rasch
I want not to allow files bigger than 1 MB.. Why I need to change my 2MB limit???
Jonathan
Since your setting is at 2MB, PHP will completely ignore any attempts to upload anything larger than 2MB, which is why you're not seeing the error message at all - because nothing happens when you submit a form with a file larger than the 2MB setting.
John Rasch
A: 

Above this line:

if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
        $sizeOK = true;
}

Put this:

echo '<pre>' . printr($_FILES) . </pre>;

That will show you what is inside the FILES array, and should make this pretty simple to debug. Try uploading the AVI with the above line added to your script.

karim79
It should be print_r($_FILES), not printr($FILES).
Peter
My bad, will fix it now
karim79
A: 

I'd also suggest that you don't believe the mime type. Sometimes people have .png or .gif file that have been renamed to .jpg, or they could upload incorrect files intentionally. Use getimagesize to check if these are valid jpeg images.

Alex JL
MIME dont check just the file extension... I think...
Jonathan
The browsers I've tested seem to set the mime type, and it's based on the file extension. They don't do real tests as to image format, that's way too elaborate. Trust me, I've built upload systems for images from scratch in PHP.
Alex JL
A: 

As john Rasch mentioned above, any file above the php.ini max_upload_filesize will not process at all. so you'll have no chance to test the error for you. you have to assume it was not uploaded and validate it if it was.


now that I understand your scenario better I think this is what you can do:

// at the top of your script
$upload_success = FALSE;


// when successfully detected upload
$upload_success = TRUE;



// if successful upload code is never run
$display_error = "File not uploaded, may be too large a file, "
.    "please upload less than 1MB"
;
print $display_error;


main point being:

You can't always detect upload files that are too big because they get cut off at a level deeper than where the scripts run.

Fire Crow
Checked the php.ini... I have 2MB limit... ;Maximum allowed size for uploaded files.upload_max_filesize = 2MSo, thats not the problem....
Jonathan
? I think you just demonstrated that that is the problem. unless your avi is between 1 and 2mb which I doubt, most avi files are well above 2mb, we have a flash video uploader and have set out max to 18
Fire Crow
Yes, my avi file is 600MB and I don t want to allow the upload.. I want a 1MB limit.. Why I need to set a bigger maximum if I just want 1 MB limit??
Jonathan
if you go above the 2MB php.ini limit the PHP script may not even detect a file was uploaded because it gets cut off at the server level by the max_upload_file_size in php.ini
Fire Crow
A: 

Don't forget, when uploading files, that there are actually two directives you need to pay attention to in php.ini. One is upload_max_filesize, but the other is post_max_size. Generally, post_max_size should at least be equal to, and probably greater than, upload_max_filesize. You can't upload a file greater than post_max_size, regardless of what you set your upload_max_filesize.

An AVI file won't match the mime types you have listed in your permitted array. After doing your $sizeOK and $typeOK checks, check to see what values they hold, and how your script handles those values. That might hold the key to the behavior of your script.

Peter