dougan7657 2016-06-04 09:41
浏览 31
已采纳

PHP图片上传代码无法正常工作

I have a php image upload code that doesn't upload the image.

PHP

if (isset($_FILES['file']) && $_FILES['file']['error']==0) {
      $files = $_FILES['file'];
     echo $tmp_name = $files['tmp_name'];
     $moved=move_uploaded_file($tmp_name,"../a.jpg");
     if($moved){        
     echo'Done!';
     }
}else{
   echo 'Error uploading, code '.$_FILES['file']['error'];
}

HTML

<form method="post" enctype="multipart/form-data" action="samefile.php">
      <input type="file" name="file">
      <input type="file" name="file">
      <input type="submit" value="submit">
</form>

This is the simplified form of my code. In the real scenario there are multiple upload button but only one button can be used at once.

The problem is that it shows the error Error uploading, code 4.
Please help.
Thanks

  • 写回答

1条回答 默认 最新

  • duankuaiwang2706 2016-06-04 10:17
    关注

    Try this code, this will work for you to upload multiple files you need to use name="file[]" instead of name="file" this will submit all the files in array and you can loop each file using for loop in php file..

    <form method="post" enctype="multipart/form-data" action="samefile.php">
          <input type="file" name="file[]" >
          <input type="file" name="file[]" >
          <input type="submit" value="submit">
    </form>
    

    In php file

    <?php 
    if (isset($_FILES['file']))
    {
        $total = count($_FILES['file']['name']);
        for($i=0; $i < $total; $i++)// Loop for each file
        { 
          $tmp_name = $_FILES['file']['tmp_name'][$i];
          if ($tmp_name != "")
          {
            $targetFile = "../" . $_FILES['file']['name'][$i];
            //Upload the file 
            if(move_uploaded_file($tmp_name,$targetFile)) 
            {
              echo'Done!';
            }
          }
        }
    }   
    

    ?>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式