简体   繁体   English

使用php脚本上传多个文件

[英]Upload multiple files with a php script

I've been modify this php script but it won't work, it always fail. 我已经修改了这个php脚本,但是它不起作用,总是失败。 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. 它设法创建了文件夹,但是无法将文件从临时文件夹移到右侧,函数move_uploaded_file始终返回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: 这是html形式:

  <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". 我知道这种形式有点烂,但是我不喜欢带有“ 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

    <?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表单应如下所示:

HTML 的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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