0

我正在尝试将多个文件上传到一个目录。我“编写”的代码适用于一个文件,但是当我尝试上传多个文件时它不起作用。我试图确定故障在哪里,我相信它与计数有关。尽管当我尝试回显正在计算多少文件时,无论我选择了多少文件,我总是得到“1”。我知道这仅适用于一个文件,因为我返回的变量始终为“1”,因此仅适用于一个文件。

收集我正在使用带有 post 方法 html 的 html 表单的文件:

<form id="upload" action="upload.php" method="POST" enctype="multipart/form-data">

<fieldset>
<legend>HTML File Upload</legend>

<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" />

<div>
    <label for="fileselect">Files to upload:</label>
    <input type="file" id="fileselect" name="fileselect[]" multiple="multiple" />
    <div id="filedrag">or drop files here</div>
</div>

<div id="submit">
    <button type="submit">Upload Files</button>
</div>

</fieldset>

</form>

我使用的 javascript 启用了拖放功能,来自: http ://www.sitepoint.com/html5-file-drag-and-drop/

罪魁祸首PHP:

if(isset($_FILES['fileselect']['tmp_name'])) {
    // Number of uploaded files
    $num_files = count($_FILES['fileselect']['tmp_name']);
    echo $num_files;
    /** loop through the array of files ***/
    for($i=0; $i <= $num_files;$i++) {
        // check if there is a file in the array
        if(!is_uploaded_file($_FILES['fileselect']['tmp_name'][$i])) {
            $messages[] = 'No file uploaded';
        }
        else {
            $unique = substr(number_format(time() * rand(),0,'',''),0,10);

            $newImg = "img".$unique;

            $filename  = basename($_FILES['file']['name'][$i]);
            $extension = pathinfo($filename, PATHINFO_EXTENSION);
            //$new       = md5($filename).'.'.$extension;

            $the_file_type = $_FILES['fileselect']['type'][$i];
            $the_file_size = $_FILES['fileselect']['size'][$i] / 1024;
            $the_file_name = $_FILES['fileselect']['name'][$i];

            $ok=1;
            //This is our size condition 
            if ($uploaded_size > 350000) { 
                $messages[] =  "Your file is too large.<br>"; 
                $ok=0; 
            } 

            //This is our limit file type condition 
            if ($uploaded_type =="text/php") { 
                $messages[] =  "No PHP files<br>"; 
                $ok=0; 
            } 

            //Here we check that $ok was not set to 0 by an error 
            if ($ok==0) { 
                $messages[] =  "Sorry your file was not uploaded"; 
            } 

            //If everything is ok we try to upload it 
            else { 
                if(move_uploaded_file($_FILES['fileselect']['tmp_name'][$i], "uploads/{$newImg}")) { 
                    echo "The file ". basename( $_FILES['uploadedfile']['name'][$i]). " has been uploaded";
                    echo "<br />";
                    //echo $the_file_type;
                    //echo "<br />";
                    //echo $unique;
                    //echo "<span>schmeckle!</span>";
                    //echo "<br />";
                    //echo $the_file_size;
                    //echo "<br />";
                    //echo $the_file_name;

                    $insertSQL = "INSERT INTO interviews_media_images SET ";
                        $insertSQL .= "fileType='$the_file_type', ";
                        $insertSQL .= "fileRef='$newImg', ";
                        $insertSQL .= "fileSize='$the_file_size', ";
                        $insertSQL .= "fileName='$the_file_name' ";

                    echo $insertSQL;

                    //mysql_query( $insertSQL);

                    //echo mysql_error();

                } else { 
                    $messages[] = "Sorry, there was a problem uploading your file."; 
                } 
            }
        }
    }
}
4

2 回答 2

0

在我的情况下,您的代码正在运行,并且 $num_files 在计数后返回真值。

另一件事是从 for 循环中删除 ' = ' 并像这样编写

for( $i=0; $i<$num_files; $i++)

如果你使用$i <= $num_files它会给你一个通知。

假设您正在选择 3 个文件并且您正在设置$i <= $num_files

所以 for 循环从 0 开始,当你选择 3 个文件时,它会持续 4 次

0 1 2 3

您可以看到循环持续了 4 次,但它没有文件

$_FILES['filename']['tmp_name'][3].

我希望这能帮到您...

对不起我的英语不好。

于 2014-10-05T11:11:24.937 回答
0

我创建了一个内容如下的文件,运行 PHP 版本 5.5.15,它的计数正确:

<?php
if(isset($_FILES['fileselect']['tmp_name']))
{
    echo "<pre>"; print_r($_FILES); echo "</pre>";
    echo "Number of files uploaded: " . count($_FILES['fileselect']['name']);
}
?>

<form id="upload" action="files_upload.php" method="POST" enctype="multipart/form-data">

<fieldset>
<legend>HTML File Upload</legend>

<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" />

<div>
    <label for="fileselect">Files to upload:</label>
    <input type="file" id="fileselect" name="fileselect[]" multiple="multiple" />
    <div id="filedrag">or drop files here</div>
</div>

<div id="submit">
    <button type="submit">Upload Files</button>
</div>

</fieldset>

</form>
于 2014-10-05T08:28:00.733 回答