0

My form has a input file type field.

<input type="file" name="file" size="80" value="">

when submitted and this field is empty the file upload part of my php script should be skipped. This does not seem to be happening. What I am getting is the wrong file type message popping. Nothing was populated in the field on the form so why is my if/else statement not being followed? What am I doing wrong?

<?php
// connect to datebase
require "master.db.php";


// this if(empty statement is not working?
// should be checking to see if the form field 'file' is populated if so check file
// type if not skip and update sql database with information in form field 'test'
if(!empty($_FILES))
{
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 2097152))
    {
        // code here works - function is to upload the file - not part of my current problem
    }

    // currently this else statement is displayed regardless of 'file' input
    // wrong function for its intended purpose
    else
    {
        // wrong file type
        echo "Invalid file <br />";
        echo "Tryed to upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb Max 2Mb <br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
        die;
    }
}
else 
{
// update other data in mysql database
$sql="UPDATE `characters` SET test='$test' WHERE ID='$id'";
$result=mysql_query($sql);

    // if successfully updated mysql.
    if($result){
        echo "<b>Update successful</b> <br />";
        echo "<a href='test.html'>View result</a>";
        touch('master.html');
        clearstatcache();
    }
    else
    {
        echo "Whoops: " . mysql_error(); ;
    }
    mysql_close();
}
?>
6
  • instead of empty try isset() Commented Jun 9, 2012 at 14:48
  • so remove if(!empty($_FILES)) and replace with if(isset($_FILES)) ? Commented Jun 9, 2012 at 14:49
  • yes, although that might still not work Commented Jun 9, 2012 at 14:51
  • sadly that did not solve my problem Commented Jun 9, 2012 at 14:52
  • stackoverflow.com/a/10961956/1438194 Commented Jun 9, 2012 at 14:53

2 Answers 2

2

The browser will still send the input value, even though the value is empty. As a consequence, PHP will populate the $_FILES array with something like:

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

)

So instead of checking with isset/empty, I guess you should use PHP's native is_uploaded_file method. See this SO post for more info on checking if an optional file input was provided.

I'll try to clarify my answer. The reference I just gave suggests using file_exists and is_uploaded_file. According to the PHP manual only using is_uploaded_file is sufficient. In fact, I can imagine is_uploaded_file looks up the file anyway (so internally already does something like file_exists).

Please note that you really should use the tmp_name-key instead of the name-key. The 'name' key specifies the name of the file on the client's PC, but the tmp_name specifies the name on the server. There are situations when the server renames the file and therefore differs from the name on the users PC.

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

7 Comments

so take out if(!empty($_FILES)) and replace with something like if(!file_exists($_FILES['myflie']['tmp_name']) || !is_uploaded_file($_FILES['myflie']['tmp_name'])) { echo 'No upload'; }
so im trying if(!file_exists($_FILES['file']['name']) || !is_uploaded_file($_FILES['file']['name'])) { echo 'No upload'; } and I will change around my if else
I think file_exists and is_uploaded_file is double, only using is_uploaded_file is sufficient. So you could use either: if (is_uploaded_file($_FILES['file']['tmp_name'])) or the negation of the predicate. Please note that in your last comment you used the name-key instead of the tmp_name-key. For proper working, you should feed is_uploaded_file with the tmp_name key!
No joy, now the file field is skipped no matter if its filled or not...hummm
Ok, but my php script moves the image from the tmp_name to the folder once the upload is done so doesnt that mean the tmp_name is no longer on the server? Also, if the field is blank and no image is on the server to start with, what would is_uploaded_file check against?
|
0

please be sure to add

enctype="multipart/form-data"

attribute in you form tag . and follow @Martijn guidelines check the below code, it must work. be sure to customize .

 <?php
 // connect to datebase
 require "master.db.php";


 // this if(empty statement is not working?
 // should be checking to see if the form field 'file' is populated if so check file
 // type if not skip and update sql database with information in form field 'test'

 if(!empty($_FILES)) // MEANS form is valid ( have valid file and other data) 
 {
$sql="UPDATE `characters` SET test='$test' WHERE ID='$id'";
$result=mysql_query($sql);

  if (((($_FILES["file"]["type"] == "image/gif")
  || ($_FILES["file"]["type"] == "image/jpeg")
  || ($_FILES["file"]["type"] == "image/pjpeg"))
  && ($_FILES["file"]["size"] < 2097152)) && $result ))
{
    // code you need to execute
    echo "<b>Update successful</b> <br />";
    echo "<a href='test.html'>View result</a>";
    touch('master.html');
    clearstatcache();
   }

   else
   {
    // wrong file type
    echo "Invalid file <br />";
    echo "Tryed to upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb Max 2Mb <br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    die;
    }
    }
   else 
   { 
echo "data is not valid";
    }
  ?>

1 Comment

@webmasteralexl : hey have to tried the above code,i edited it according to your needs .

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.