0

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

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

2 回答 2

2

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

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

)

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

我会尽力澄清我的答案。我刚刚给出的参考建议使用 file_exists 和 is_uploaded_file。根据 PHP 手册,仅使用 is_uploaded_file 就足够了。事实上,我可以想象 is_uploaded_file 无论如何都会查找文件(所以内部已经做了类似 file_exists 的操作)。

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

于 2012-06-09T14:49:57.533 回答
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";
    }
  ?>
于 2012-06-09T14:55:36.570 回答