Max size query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Max size query

Post by Addos »

Hi,
I’m trying to produce an error if a file size goes over 2MB using the following:

Code: Select all

if ($_FILES['fupload']['size'] > 25000) {
     $message_fileSize = '<b>SORRY! file size exceeds 2MB</b>';
echo $message_fileSize;
However this error is not being produced when a file goes over 2MB and just doesn’t upload either. I have run:

Code: Select all

print "error: ". $_FILES['fupload']['error'];
and this gives me 1 which suggests that the limit has been exceeded (when I deliberately upload 3MB file) in the .ini file which I have no access to so I’m wondering why I can’t show an error when I test the file being uploaded.

When I upload anything under 2MB the file uploads ok so my script is ok in that area.

Any thoughts?
Thanks
p3rk5
Forum Commoner
Posts: 34
Joined: Thu Jan 29, 2009 10:19 pm

Re: Max size query

Post by p3rk5 »

The default values in the .ini files only allow you to upload up to 2MB. You would have to change max_upload_size and max_post_size. Try this link: http://exaltations.net/weblogs/christef ... else_works
NOmeR1
Forum Newbie
Posts: 8
Joined: Sat Feb 14, 2009 8:32 am

Re: Max size query

Post by NOmeR1 »

What returns $_FILES['fupload']['size'] when size of file > 2 mb?
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Re: Max size query

Post by Addos »

Thanks for the reply.
I understand that the ini file has a max of 2MB and that is ok but I just wanted to get this error to show if it's over 2MB.

The script I'm using is below:

Code: Select all

if ($_FILES['fupload']['type'] == "audio/mpeg") {
  // Test for file size
 if ($_FILES['fupload']['size'] > 25000) {
     $message_fileSize = '<b>SORRY! file size exceeds 2MB</b>';
 } else {
    $source = $_FILES['fupload']['tmp_name'];
    $target = "audio/mp3/" .$_FILES['fupload']['name'];
etc etc
And in the Form I have:

Code: Select all

 <?php  if (isset($message_fileSize) && !empty($message_fileSize)) {
          echo $message_fileSize ;  ?>  
But it seems to simply ignore this conditional statement.

$_FILES['fupload']['size'] when size of file > 2 mb? actually returns a 0 value.

Thanks
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Max size query

Post by pickle »

Well, 25000 bytes = 25KB, not 2MB

When the user tries to upload the file, look in $_FILES['fupload']['error']. If that value is equal to UPLOAD_ERR_INI_SIZE, the problem was the file was too big. PHP does the size checking for you - no need to duplicate functionality.

I'd recommend taking a look at the PHP docs for handling HTTP file uploads
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply