Jump to content

Problem Checking File Size if else statement


kasitzboym

Recommended Posts

I am trying to check to see if a file size is acceptable before an upload but cant seem to get the right result no matter what i do.

 

if i have my if statement below coded like this

 

    if($_FILES["upload"]["size"] < 1024000){
        echo'entered if statement<br />';
    }
    else{
        echo'entered else statement<br />';
    }

 

Then i always am entereing the if statememnt but if i have my if statement like this

 

 

    if($_FILES["upload"]["size"] > 1024000){
        echo'entered if statement<br />';
    }
    else{
        echo'entered else statement<br />';
    }

 

then i always seem to enter the else staement.

 

I have tried with a VIREITY of differnt size files some from like 2kb to 10mb...

 

i believe somewhere near the > or < is my problem but i dont seem to see it

Link to comment
Share on other sites

How about using the filesize() function:

 

 

$filename = 'somefile.txt';

if(filesize($filename) < 1024000){
        echo'entered if statement<br />';
    }
    else{
        echo'entered else statement<br />';
    }

 

Just use variables or a form to get the file

Link to comment
Share on other sites

Thanks but im having more trouble with this than i am with my if else statement... If i take out the statement and just echo $_FILES["upload"]["size"] then it prints out the file size every time its just when i have it in an if statement that it has trouble. Any other ideas? i really would like to use this setup.

Link to comment
Share on other sites

Thanks but im having more trouble with this than i am with my if else statement... If i take out the statement and just echo $_FILES["upload"]["size"] then it prints out the file size every time its just when i have it in an if statement that it has trouble. Any other ideas? i really would like to use this setup.

 

Hmm not sure. If you are getting the right size when you use echo, then how about putting it in a variable, then calling the variable?

 

$file = $_FILES["upload"]["size"];

if($file < 1024000){
        echo'entered if statement<br />';
}
    else{
        echo'entered else statement<br />';
    }

Link to comment
Share on other sites

You know what i figured figured it out... i dont know why but it didnt like the number in the if statement i changed it to this and i had the variable set like $size = $_FILES["upload"]["size"]; i just type it like that when i was on here... but changing it to a variable seamed to have worked... ill have to do more reading on it maybe it is a bug in PHP they haven't found yet or something...  But Anyway thanks for the help!!!

 


$size = $_FILES["upload"]["size"];
$max_file_size = 1024000;

    if($size > $max_file_size){
        echo'entered if statement<br />';
    }
    else{
        echo'entered else statement<br />';
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.