0

我有一个 html 表单,我用它来上传图片到服务器

    <form id="form1" name="form1" method="post" action="testimage.php" enctype="multipart/form-data" >   <table width="1000" border="0">
    <tr>
    <td width="108">ID</td>
    <td width="229">
      <input type="text" name="id" id="id" value="<?php if(isset($_GET['editcat'])){echo $_GET['editcat'] ;}; ?>" tabindex="1" />
   </td>
   <tr>
    <td>Image</td>
    <td>:</td>
    <td><input type="file" name="image" id="image" tabindex="7"  /></td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>User</td>
    <td>:</td>
    <td><input type="text" name="user" id="user" value="<?php echo $user ; ?>" tabindex="8" /></td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><?php if(isset($_GET['editcat'])){ ?> <input name="editcat" type="hidden" value="set" /> <?php }?> </td>
    <td><input type="submit" name="submit" id="submit" value="Add/Edit" tabindex="9" />                        </td>
    <td>&nbsp;</td>
  </tr>
</table> </form>

我通过 php 文件检索此表单数据

       <?php session_start();
       $user_name = $_SESSION['username'];
       $user_pass = $_SESSION['password'];
       if ( $user_name == '' ) {
       header('location:login.php');
       exit();
       }
       ?>
       <?php require_once('connection.php');?>
        <?php require_once('function.php');?>
       <?php 
       $err = 0;
       if(!isset($_SESSION['msgback'])){
       $_SESSION['msgback'] = array();
       }else{
   unset($_SESSION['msgback']);
   $_SESSION['msgback'] = array();
       }
       if(isset($_POST['id'])){
   $id = $_POST['id'];
       }else{
   $id = "";
       }if(isset($_POST['user'])){
   $user = $_POST['user'];
       }else{
   $user="";
        }
       if(!empty($_FILES['image'])){
       $name= basename($_FILES['image']['name']); 
       move_uploaded_file($_FILES["image"]                                  ["tmp_name"],"Hants/admin/uploads/fron_sect/$name")or die(mysql_error());   
        }
         ?>
        <?php
        /////          
        if(isset($_POST['editcat'])){
     if($err == 0){
    if($_POST['id'] == ""){ $_SESSION['msgback'][] = "empty id"; }
        if(empty($_SESSION['msgback'])){
        $user_query = "
        UPDATE  
         pro_category 
        SET  
        `image`='$name',
    `update_user`='$user'
    WHERE  
        `id`='$id'";
        $user_result = mysql_query($user_query) or die(mysql_error());
    $_SESSION['msgback'][] = "Product category is updated sucesfully.";
    header('location:product_cat.php');
    }
   } 
    } 

    ////////Inserting a new Iteme////////
    if(!isset($_POST['editcat'])){ echo "editcat not set" ."<br/>";
   if($err == 0){ 
    if($_POST['id'] == "") {    
         $_SESSION['msgback'][] = "empty id";
        }
        if(empty($_SESSION['msgback'])){
          $user_query = "INSERT                            INTO `pro_category` (`section_id`,`category_name`,`description`,`position`,`visible`,`image`,`pro_view`,`update_user`) VALUES('{$secid}','{$catnam}','{$descrptn}','{$pstion}','{$visble}','{$name}','{$proview}','{$user}')
                                ";
$user_result = mysql_query($user_query) or die(mysql_error());
//if(mysql_affected_rows()){
//$_SESSION['user_name'] = $username;
$_SESSION['msgback'][] = "Successfuly Added";
header('location:product_cat.php');
                    //}
                }else{
                $_SESSION['msgback'][] = "Not Successfuly Added";
                    header('location:product_cat.php');
                //redirect_to("product.php?msgback=msgerr");
                    }
    }
}
?>

当我选择要上传的图像时,此代码运行良好。但在某些情况下,我不需要上传图片,但表单字段的其余部分必须上传到服务器。当我尝试在不选择图像的情况下提交表单时,它向我显示一个空白页面并且没有任何反应。我无法弄清楚错误在哪里。如果有人可以帮助我。

4

2 回答 2

1

更新您的代码以添加此检查以查看文件是否实际上传:

if ($_FILES['image']['error'] == UPLOAD_ERR_OK) {
于 2012-05-22T09:05:53.470 回答
0

您的问题可能出if(!empty($_FILES['image'])){在您所说的上传图片时效果很好,但是当您不上传图片时会收到一个空白页面。

print_r($_FILES['image])当您不上传图像并查看正在发送的内容时,我会这样做。我怀疑你empty()还在捡东西,也许是一个数组。

也按照建议做 davidethell。

于 2012-05-22T09:08:31.927 回答