dongzhao8233 2016-11-24 14:38
浏览 21

如何逐个上传多个文件

I need to upload a lot of images at once (e.g. 200 small images) through the browser. I have this code:

<?php
if(isset($_POST['Upload_files']) and $_SERVER['REQUEST_METHOD'] == "POST"){

  echo count($_FILES['files']['name']); // Even if I upload more than 20 files, it still displays 20.

  $target_directory = "upload/";  
  foreach ($_FILES['files']['name'] as $file_number => $file_name) {
    $file = $_FILES["files"]["tmp_name"][$file_number];
    if (mime_content_type($file) != 'image/png' && mime_content_type($file) != 'image/jpeg')
      { $error[] = 'File '.$file_name.' is not in JPG / PNG format!'; continue; }
    else
      { if(move_uploaded_file($file, $target_directory.$file_name)) {$count_of_upload_file++;}}
  }
}

// If files were uploaded 
if($count_of_upload_file != 0){echo 'Your files ('.$count_of_upload_file.' ) were successfully uploaded.';}

// If there was an error
if (isset($error)) {foreach ($error as $display_error) {echo '- '.$display_error.'<br />';}}
?>

<form action="" method="post" enctype="multipart/form-data">
      <input type="file" name="files[]" multiple="multiple" accept="image/*">
      <input type="submit" name="Upload_files" value="Upload files">
</form>   

And it works. However my provider set the 'max_file_uploads' to 20 (and I'm not able to change it). (The Uploadify extension is working for more than 20 files, but I'd like to have my own small solution.) So I presume I need to add here something, which instead of 200 files at once uploads 200 files one by one (or twenty by twenty). But I don't know how to do it. (To use AJAX? -- I never used it, so I really don't know.) Thank you!

  • 写回答

1条回答 默认 最新

  • dongsuoying9059 2016-11-24 16:32
    关注

    If you don't want to use ajax, you need a loop to receive files

    for($i = 0; $i < count($_FILES['files']['tmp_name']); $i++){
        $tmp = $_FILES['files']['tmp_name'][$i];
        $name = md5(microtime());
    
     if(move_uploaded_file($tmp, "dir/$name.jpg")){
        echo "Uploaded successfully";
     }else{
        echo "failed";
     }
    }
    

    In your form you must name all your input fields as files[], example:

    <form action="" method="post" enctype="multipart/form-data">
          <input type="file" name="files[]" multiple="multiple" accept="image/*">
          <input type="file" name="files[]" multiple="multiple" accept="image/*">
          <input type="file" name="files[]" multiple="multiple" accept="image/*">
          <input type="submit" name="Upload_files" value="Upload files">
    </form>
    

    Or if you want to use ajax here is an example: Upload multiple image using AJAX, PHP and jQuery

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大