1

Hello for some reason I can't get my program to see if my file is empty or not if something is selected it should not go through but it is. Here is the code

if (empty($_FILES['file'] ) )
    {
        echo "testing";
        $seterror =1;
        returnBack();

    } 
16
  • include var_dump($_FILES) or print_r($_FILES) Commented Sep 4, 2012 at 23:53
  • For you to see or in my program? Commented Sep 4, 2012 at 23:53
  • what exactly isn't working? try using "var_dump($_FILES);" & "echo $_FILES['file'];" to make sure that it's what you expect. Commented Sep 4, 2012 at 23:54
  • i would not check the whole array, but the file name Commented Sep 4, 2012 at 23:54
  • 1
    yeah, but what's not working? what are you having a problem with? is it echoing "testing" or not with that $_FILE array? Commented Sep 5, 2012 at 0:06

6 Answers 6

2

What do you get if you output the files array? (i.e. print_r($_FILES);)

Your code won't tell you if the contents of the file called file is empty, it will tell you if the value of $_FILES['file'] is empty, which might not be what you want. You also might not want the logic of empty - have you tried this to see if there is a difference?

if (array_key_exists('file', $_FILES)) {
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

if works but even if I don't enter a file it still goes through.
2

From the var_dump($_FILES) you provided in the comments, it looks like it isn't echoing "testing" (i.e. it's not working) because $_FILE['file'] isn't empty.

from what you provided me:

$_FILES['file'] = array(5) { ["name"]=> string(0) "" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(4) ["size"]=> int(0) }

I'm not completely sure how empty() handles arrays (the documentation just says it will return true for an empty array), but at the very least you'd need to either get rid of $_FILES['file']['error'] (which isn't empty since it's an int(4)), or use empty() to check something like $_FILES['file']['name']

so, using my suggestion, here's how the code would look:

if (empty($_FILES['file']['name'])) {
    echo "testing";
    $seterror = 1;
    returnBack();    
}

or (since there might be something wrong with my understanding of PHP arrays), try this:

if(empty($_FILES['file']['name'])) {
    echo "testing";
    $seterror = 1;
    returnBack();
}

4 Comments

The problem is with using this It always see that the file is not empty and i have so clue why
@DavidBiga - sorry, I made an error in my code (which I just fixed), try with it now :)
o.o that should work...could you do var_dump($_FILES['file']['name']); ?
just fixed it :) thanks for the help but I needed to use error instead of name
1

The language construct empty() and arrays in PHP do not mix well. In addition, if you have a <input type='file' name='file' /> in your form, you will get a $_FILES[] array. You should be doing if( !array_key_exists('file', $_FILES) ) { // no file uploaded } to start with.

In fact, you should be doing several checks:

  • If the file element is populated in $_FILES
  • If there is no error (that is, $_FILES['file']['error'] == 0)
  • If there is a filename for the uploaded file's temporary location
  • If PHP believes it is an uploaded file and/or it can move it

Only of all of those are true will you have a file to process. Otherwise you don't.

I've found following these steps religiously on a file upload handler to be thoroughly reliable.

2 Comments

Only when I do not select a file.
$_FILES['file']['error'] will be 0 if there is a file uploaded. If it is not 0 at that step (or it doesn't exist in the array), you do not have a file.
1

If you are testing for empty files here is the code

$file = 'test.txt';

if(filesize($file) == NULL) {
    echo "empty"; 
}

1 Comment

worked except even if it does have a file it still goes through
1

I think you can try this

if(empty($_FILES['file']['name']))
{
    // not uploaded
}
else
{
    // uploded
}

Comments

0

Okay so what I had to do was check to see if it error - so if it did it would not go through because it would mean its empty

code:

if (!empty($_FILES['file']['error'] ) )
    {

        echo "file";
        $seterror =1;
        returnBack();

    } 

Thank you so much for the help guys!

1 Comment

You should be checking that $_FILES['file']['error'] is or is not 0. The construct empty() just happens to work because a value of 0 is interpreted as "empty" by PHP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.