php if else与表单字段的错误

时间:2012-06-09 14:45:17

标签: php forms if-statement

我的表单有一个输入文件类型字段。

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

提交时此字段为空,应跳过我的php脚本的文件上传部分。这似乎没有发生。我得到的是错误的文件类型消息弹出。表单上的字段中没有填充任何内容,为什么我的if / else语句没有被遵循?我做错了什么?

<?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();
}
?>

2 个答案:

答案 0 :(得分:2)

即使值为空,浏览器仍会发送输入值。因此,PHP将使用以下内容填充$ _FILES数组:

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

)

因此,我猜你应该使用PHP的本机is_uploaded_file方法,而不是使用isset / empty进行检查。有关检查是否提供了可选文件输入的详细信息,请参阅this SO帖子。

我会试着澄清我的答案。我刚才给出的引用建议使用file_exists和is_uploaded_file。根据PHP手册,只使用is_uploaded_file就足够了。事实上,我可以想象is_uploaded_file无论如何都要查找文件(所以内部已经做了类似file_exists的事情)。

请注意,您确实应该使用tmp_name-key而不是name-key。这个名字&#39; key指定客户端PC上文件的名称,但tmp_name指定服务器上的名称。在某些情况下,服务器会重命名文件,因此与用户PC上的名称不同。

答案 1 :(得分:0)

请务必添加

enctype="multipart/form-data"
您在表单标签中的

属性。并遵循@Martijn指南 检查下面的代码,它必须工作。一定要定制。

 <?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";
    }
  ?>