Restrict image by file size

See all posts Reply

Restrict image by file size new!
by Ben, 14 years, 11 months ago
Hi,

Can you please provide an example on how to restrict the upload if it is above a certain size?

I do this after the $handle->uploaded (im not sure if this is correct), however i get an exception thrown:

/upload.class.php(3036): unlink('73278a4a86960ee...')

My code is as follows:
if ($handle->uploaded) {
  $handle->file_max_size = '50';

Best regards, Ben.Reply
Re: Restrict image by file size new!
by colin, 14 years, 11 months ago
Can you copy here the logs from the class?

Besides, file_max_size is in bytes, and should be an integer. So 50 is probably way to small.

Try something like:
$handle->file_max_size = 5000;
Reply
Re: Restrict image by file size new!
by Ben, 14 years, 11 months ago
Hi Colin,

Thanks for the response. Yes, appreciate the value is small, the idea being that it will always fail, as I wanted to test catching the error in my app.

The issue i have is that once i add the $handle->file_max_size code, it fails. The log is as follows:

system information
- class version : 0.27
- GD version : 2.0.34
- supported image types : png jpg gif bmp
- open_basedir : no restriction
- language : en_GB
source is an uploaded file
- upload OK
- file name OK
- MIME type detected as image/jpeg by PHP getimagesize() function
- source variables
file_src_name : DSCF0073.jpg
file_src_name_body : DSCF0073
file_src_name_ext : jpg
file_src_pathname : /tmp/phpL01Kyz
file_src_mime : image/jpeg
file_src_size : 69542 (max= 10485760)
file_src_error : 0
- source file is an image
image_src_x : 500
image_src_y : 375
image_src_pixels : 187500
image_src_type : jpg
image_src_bits : 8


My own logging shows:

FATAL: exception with message 'unlink(67e103b0761e60683e83c559be18d40c.) [function.unlink]: No such file or directory' in /home/boardh/web/index.php:80 Stack trace: #0 [internal function]: ErrorHandler(2, 'unlink(67e103b0...', '/home/boardh/ap...', 3036, Array) #1 /home/boardh/application/lib/upload.class.php(3036): unlink('67e103b0761e606...') #2 /home/boardh/application/controllers/photocontroller.php(92): upload->process('/home/boardh/we...') #3 [internal function]: photoController->upload() #4 /home/boardh/application/models/frontcontroller.php(78): ReflectionMethod->invoke(Object(photoController)) #5 /home/boardh/web/index.php(87): FrontController->route() #6 {main}

So it looks like im getting a "No such file or directory" caused by line 3036 of the class -
@unlink($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext);

Any thoughts would eb appreciated, its got me stumped!!

Best regards, Ben.Reply
Re: Restrict image by file size new!
by colin, 14 years, 11 months ago
I am not too sure what can be the cause. The line 3036 is part of the code which checks that the destination directory is writable. It attemps to create a temp file (which it seems to have done with success, and then deletes it on line 3036).

Can you replace this line with the following?
echo $this->log; 
unlink($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext);

So we should have more information.

And are you sure it fails only when you use file_max_size code?Reply
Re: Restrict image by file size new!
by Ben, 14 years, 11 months ago
Hi Colin,

Many thanks for the reply. Looking at the log as per you suggestion above shows no errors (see output below). As soon as i add in the file size restriction it dies with the error in my first post. Interestingly enough, at line 3036, if i echo $this->file_dst_path and this->file_dst_name_ext, they are both empty. Only the $hash is populated.

Im going to pull the code out of the MVC framework and run it stand alone for further testing. It must be something in my framework causing the issue.

Best regards, Ben.

system information
- class version : 0.27
- GD version : 2.0.34
- supported image types : png jpg gif bmp
- open_basedir : no restriction
- language : en_GB
source is an uploaded file
- upload OK
- file name OK
- MIME type detected as image/jpeg by PHP getimagesize() function
- source variables
file_src_name : LGIM0134.jpg
file_src_name_body : LGIM0134
file_src_name_ext : jpg
file_src_pathname : /tmp/php1aPrgK
file_src_mime : image/jpeg
file_src_size : 211818 (max= 10485760)
file_src_error : 0
- source file is an image
image_src_x : 1024
image_src_y : 768
image_src_pixels : 786432
image_src_type : jpg
image_src_bits : 8
process file to /home/boardh/web/users/test/
- file mime OK : image/jpeg
FATAL:
Reply
Re: Restrict image by file size new!
by colin, 14 years, 11 months ago
Yes, I now see what is going on.

Basically, if you set $foo->file_max_size to a very small value, $foo->processed becomes false, and you have an error in $foo->error which will say "File too big"

But the code has a bug, and so it carries on processing the file even if $foo->processed is false.

I will fix it in the next release. I will try to have a test release before the end of the dayReply
Re: Restrict image by file size new!
by colin, 14 years, 11 months ago
Can you try the following test release? 0.28RC1

It should solve the problem. If the class doesn't pass a check, such as the maximum file size check, the processing will stop, and the error will be set. The error will also be logged for convenience purposes.

Let me know.Reply
Re: Restrict image by file size new!
by Ben, 14 years, 10 months ago
Superb, thanks Colin! The 0.28RC1 works great with the very small value.

Really appreciate the assistance here, save me many many hours!

Best regards, Ben.Reply