php如果空跳过动作

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

标签: php mysql file-upload is-empty

我有一个带字段的表单

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

如果此字段为空我想跳过尝试上传图像(因为没有)并且只更新任何已更改的信息。如果有图像,请上传并更新mysql数据库。

此代码仅在上传新图片时有效。添加了if(!empty,但现在当表单字段留空时,wrong file type代码会弹出?

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

if(!empty($_FILES['file']))
    {
      if ( file types )
          {
           // upload the file
          }
       else
          {
           // wrong file type
                 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 :(得分:0)

使用此

if(!$_FILES['file']){ 
     echo "file is empty" 
}

而不是

if(empty($_POST['file']))

最后

    if(!empty($_FILES['file'])){ 
     //file has something
    }
   else{
     //either file is empty or filed is not set
   }

答案 1 :(得分:0)

  <?php
  if(!empty($_FILES))
  {
    $name = $_FILES["fileToUpload"]["name"];
    $type = $_FILES["fileToUpload"]["type"];
    $size = $_FILES["fileToUpload"]["size"];

    $match = mysql_query("SELECT * FROM `tb_name` WHERE file_name='$name'");
    if(!empty($match))
     {
       while($res=mysql_fetch_array($match))
       {
         if($type == "filetype")
           {
             //upload
           }
         else
            //error
       }
     }
    else
      //file already exists
  }
  ?>