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();
}
?>
webmaster alex l
  • 663
  • 3
  • 16
  • 32

2 Answers2

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.

Community
  • 1
  • 1
Martijn
  • 5,491
  • 4
  • 33
  • 41
  • 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'; } ` – webmaster alex l Jun 09 '12 at 14:58
  • 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` – webmaster alex l Jun 09 '12 at 15:00
  • 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! – Martijn Jun 09 '12 at 15:05
  • No joy, now the file field is skipped no matter if its filled or not...hummm – webmaster alex l Jun 09 '12 at 15:05
  • 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? – webmaster alex l Jun 09 '12 at 15:08
  • WONDERFULL..(happy dance) it worked to a tea. Thanks for all the help! – webmaster alex l Jun 09 '12 at 15:11
  • I guess your PHP script moves the upload after you've used is_uploade_file to check whether something was uploaded, so that shouldn't be a problem. If the field is blank and no image is on the server, is_uploaded_file would check against nothing and return the required result: false ;) – Martijn Jun 09 '12 at 15:12
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";
    }
  ?>
Community
  • 1
  • 1
Paritosh Piplewar
  • 7,982
  • 5
  • 26
  • 41