简体   繁体   中英

upload multiple files error in 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!

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM