dongweicha6077 2015-09-21 18:36
浏览 9
已采纳

在PHP中上传多个文件错误

I create a folder for files. I am trying to upload multiple file to this new folder but, my code can't upload the array of files. I could upload one file to the new dir but not multiple. Here is my code.

HTML

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

PHP

$path = 'CreateFolder';

if (!is_dir($path)) {
    mkdir($path);
}

for ($i = 0; $i < count($_FILES["document_name"]["name"]); $i++) {


    $upload_dir = 'C:myDir\\' . $path . '\\';
    $document_url = $upload_dir . basename($_FILES["document_name"]["name"][$i]);

    echo $document_url;//test url

    $type_of_document = pathinfo($document_url, PATHINFO_EXTENSION);//any type allow

    if (move_uploaded_file($_FILES["document_name"]["tmp_name"][$i], $document_url)) {

        echo 'Uploaded!';
    } else {
        echo "Sorry, there was an error uploading your file. <br/>";

    }
}

I follow other answers that I found online. One of those answers suggest me to use 3 different names, okey cool, but I would like to learn to do it using an array and a for loop.

The only thing that I am getting back is

Sorry, there was an error uploading your file

Thank you!

  • 写回答

1条回答 默认 最新

  • douyi4991 2015-09-21 18:45
    关注

    The problem is your path. I would do it like this:

    $upload_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . $path;
    

    and $document_url :

    $document_url = $upload_dir . DIRECTORY_SEPARATOR . basename($_FILES["document_name"]["name"][$i]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?