0

我有一个带字段的表格

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

2 回答 2

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
  }
  ?>
于 2012-06-09T09:15:04.547 回答
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
   }
于 2012-06-09T03:25:32.033 回答