2

I am using this PHP Code:

foreach ($_FILES['ticket_files']['name'] as $key => $value)
{
    if(!empty($_FILES['ticket_files']))
    {

    }
}

But if the file input is blank, it still thinks that there is a file there and runs the code.

2
  • Have you tried echoing the contents of the file, to see what it's seeing? It might be pulling up something you don't expect. Commented Oct 29, 2013 at 16:09
  • 5
    empty() does not check contents of files, it checks content of variables. $_FILES['ticket_files'] points to an array of information about the file uploaded - including name - so it will never be empty if a file was selected. Commented Oct 29, 2013 at 16:10

5 Answers 5

1
foreach ($_FILES['ticket_files']['name'] as $key => $value)
{
    if (count($_FILES['ticket_files']) > 0)
    {

    }
}

Try this instead of what you have now.

Sign up to request clarification or add additional context in comments.

1 Comment

this is still doing the same thing
0

try this

foreach ($_FILES['ticket_files']['name'] as $key => $value)
{
    if(!$_FILES['ticket_files']['error'][$key])
    {
         //Do Stuff
     }
}

Comments

0

You can use this:

if (file_get_contents( $file_path )  == '')
{
    // file is empty
} 

Comments

0

You need check the contents of the file to see if it's empty.

foreach ($_FILES['ticket_files']['name'] as $key => $value)
{
    if(!empty(file_get_contents($path . $_FILES['ticket_files']['name']))
    {
         //Do Stuff
     }
}

Comments

0

If nothing is uploaded $_FILES array looks like:

Array ( [image] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )

The error code 4 [error] => 4 indicates no file was uploaded and error code 0 indicates no error and file was uploaded. You can add below check instead of empty check.

if($_FILES['ticket_files']['error']==0) {
    // file uploaded, process code here
}

Comments

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.