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!';
            }
          }
        }
    }   
    

    ?>

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

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据