PHP count()计数不正确

时间:2013-06-27 22:03:06

标签: php upload

我正在尝试将多个文件上传到目录。 我已经“编写”的代码适用于一个文件,但是当我尝试上传多个文件时,它不起作用。我试图确定故障的位置,我相信它与计数有关。虽然当我试图回显有多少文件被计算时,无论我选择了多少文件,我总是得到'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."; 
                } 
            }
        }
    }
}

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

您的代码在我的情况下运行,$ num_files在count之后返回true值。

还有一件事要从for循环中删除' = '并像这样写

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

如果你使用 $ i&lt; = $ num_files ,它会给你一个通知。

假设您正在选择3个文件而您正在设置 $ i&lt; = $ num_files

因此,循环从0开始,当您选择3个文件时,它会继续4次

0 1 2 3

你可以看到循环继续4次,但它没有文件

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

我希望这会对你有帮助......

抱歉我的英文。