简体   繁体   中英

Upload multiple files with a php script

I've been modify this php script but it won't work, it always fail. It managed to create the folder, but it fails to move the files from the temporary folder to the right one, the function move_uploaded_file return always false. This is the code:

<?php
include 'connection.php';
include '../empty.html'; 
session_start();

if(isset($_FILES['filearray'])){
    $name_array = $_FILES['filearray']['name'];
    $tmp_name_array = $_FILES['filearray']['tmp_name'];
    $type_array = $_FILES['filearray']['type'];
    $size_array = $_FILES['filearray']['size'];
    $error_array = $_FILES['filearray']['error'];

    $titlealbum=$_POST['titoloalbum']; 
    $username=$_SESSION['username']; 
    $path="../users/".$username."/".$titlealbum."/"; 
    echo $path;
    mkdir($path,0777);    

    $total=count($tmp_name_array);
    for($i=0; $i<$total; $i++){
        $rightpath=$path.$name_array[$i];
        if(move_uploaded_file($tmp_name_array[$i], $rightpath)){
                echo $name_array[$i]." upload is complete<br>";
                echo "upload completato"; 
        } else {
            echo "move_uploaded_file function failed for ".$name_array[$i]." into".$path."<br>";
        }
    }
}
else
echo "Files not found";
?>

This is the html form:

  <form id="albumform" style="display:none"  enctype="multipart/form-data" action="scripts/albumupload.php" multiple="multiple"  method="POST">
      <input type="hidden" name="MAX_FILE_SIZE" value="30000000">
      Name: <input name="titoloalbum" type="text" required><br><br>
      Cover: <input name="userfile" type="file">
     <br><br>Select your songs:<br />
     <input name="filearray[]" type="file" value="10000000" /><br />
     <input name="filearray[]" type="file" value="10000000"/><br />
     <input name="filearray[]" type="file" value="10000000"/><br />
     <input name="filearray[]" type="file" value="10000000"/><br />
     <input type="submit" value="Send files" />
    </form>

I know that this form kinda sucks, but i don't like the multiple selection with a signle "input". Thanks in advice

You have error in your code :

$total=count($tmp_name_array);

change this to

$total=count($name_array);

You are using count function with wrong varia ble.

Also remove so many file types with same name from the form. Either name them different.

<input name="filearray[]" type="file" value="10000000"/><br />

You could use the following commented algorithm for Multiple-Files upload:

PHP

    <?php
        // FILENAME: albumupload.php
        include 'connection.php';
        include '../empty.html';
        session_start();


        $filesArray     = isset( $_FILES['filesArray'] ) ? $_FILES['filesArray']        : null;
        $titleAlbum     = isset($_POST['titoloalbum'])   ? isset($_POST['titoloalbum']) : null;
        $arrFilesData   = array();

        if( $filesArray && !empty($filesArray) ){
            $arrFilesKeys       = array_keys($filesArray['name']);
            $arrFilesNames      = $filesArray['name'];
            $arrFilesTypes      = $filesArray['type'];
            $arrFilesTmpNames   = $filesArray['tmp_name'];
            $arrFilesErrors     = $filesArray['error'];
            $arrFilesSizes      = $filesArray['size'];

            foreach($arrFilesKeys as $intKey=>$strKeyName){
                $tempFileData               = new stdClass();
                $tempFileData->key          = $strKeyName;
                $tempFileData->name         = $arrFilesNames[$strKeyName];
                $tempFileData->type         = $arrFilesTypes[$strKeyName];
                $tempFileData->tmp_name     = $arrFilesTmpNames[$strKeyName];
                $tempFileData->error        = $arrFilesErrors[$strKeyName];
                $tempFileData->error        = $arrFilesSizes[$strKeyName];
                $arrFilesData[$strKeyName]  = $tempFileData;
            }

            // UPLOAD THE FILES:
            if($titleAlbum){
                $username   = trim($_SESSION['username']);
                $path       =  __DIR__ . "/../users/" . $username . "/" . $titleAlbum;

                //CREATE UPLOAD DIRECTORY IF IT DOESN'T ALREADY EXIST...
                if(!file_exists($path)){
                    mkdir($path,    0777, TRUE);
                }

                // LOOP THROUGH THE FILES OBJECT ARRAY AND PERFORM FILE-UPLOAD
                foreach($arrFilesData as $fileKey=>$objFileData){
                    $rightPath      = $path . DIRECTORY_SEPARATOR . $objFileData->name;
                    if(move_uploaded_file($objFileData->tmp_name, $rightPath)){
                        echo $objFileData->name . " upload is complete<br>";
                        echo "upload completato";
                    } else {
                        echo "move_uploaded_file function failed for ". $objFileData->name ." into". $path . "<br>";
                    }

                }
            }

        }

In this case, your HTML Form is expected to look like this:

HTML

        <form id="albumform" style=""  enctype="multipart/form-data" action="scripts/albumupload.php" method="POST">
            <input type="hidden" name="MAX_FILE_SIZE" value="30000000">
            Name: <input    name = "titoloalbum" type="text" required><br><br>
            Cover: <input   name = "filesArray[userfile]" type="file">
            <br><br>Select your songs:<br />
            <input name="filesArray[file_1]" type="file" value="" /><br />
            <input name="filesArray[file_2]" type="file" value=""/><br />
            <input name="filesArray[file_3]" type="file" value=""/><br />
            <input name="filesArray[file_4]" type="file" value=""/><br />
            <input type="submit" value="Send files" />
        </form>

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