【问题标题】:Issue uploading multiple files to server将多个文件上传到服务器的问题
【发布时间】:2012-04-02 21:04:18
【问题描述】:

我正在编写一个相当简单的文件服务器,并且在上传单个文件、将其移动到服务器上的文件夹并将有关它的信息保存在数据库中方面做得很好。现在,当我尝试修改它以接受来自单个输入字段的多个文件时,我无法让它通过第一次错误测试。

这是我的 index.php:

    <body>
       <img src="style/images/sitename.gif" alt="sitename" align="absmiddle" class="displayed" />
        <div id="sidediv">
            <ul>
                <li>Multiple files uploaded at once will return a link to a zip archive of those files.
            </ul> 
        </div><!--close the sidediv-->
        <div id="container">        
            <div id="content">                
                    <!--form starts here-->
                    <form action="upload.php" id="group" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >
                         <p id="f1_upload_process">Loading...<br/><img src="loader.gif" /><br/></p>
                         <p id="f1_upload_form" align="center"><br/>
                             <label>File:  
                                  <input name="myfile[]" type="file" size="30" multiple="multiple" />
                             </label>
                             <label>
                                 <input type="submit" name="submitBtn" class="sbtn" value="Upload" multiple="multiple" />
                             </label>
                         </p>

                         <iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
                    </form>
                    <!--form ends here-->
            </div>
             <!--<div id="footer"><a href="" target="_blank">sitename</a></div>-->
        </div>
        <div id="link"></div>            
</body>   

还有我的upload.php:

    <?php


    //database
    $username="";
    $password="";
    $database="";
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");

    $message = array();
    $result = array();
    $fileName = array();
    $ext = array();
    $tmpName = array();
    $path = array();
    $target_path = array();

    $count = count($_FILES['myfile']['name']);
    for($i=0;$i<$count;$i++)
    {   
        //file info
        $fileName[$count] = $_FILES['myfile']['name'][$count]; // Get the name of the file (including file extension).
        $ext[$count] = pathinfo($fileName[$count], PATHINFO_EXTENSION); // Get the extension from the filename.
        $tmpName[$count]  = $_FILES['myfile']['tmp_name'][$count];
        $fileSize[$count] = $_FILES['myfile']['size'][$count];
        $fileType[$count] = $_FILES['myfile']['type'][$count];  

        //file info
/*      $fileName = $myfile['name']; // Get the name of the file (including file extension).
        $ext = pathinfo($fileName, PATHINFO_EXTENSION); // Get the extension from the filename.
        $tmpName  = $myfile['tmp_name'];
        $fileSize = $myfile['size'];
        $fileType = $myfile['type'];*/

       // Edit upload location here
        $destination_path = './files/';
        $allowed_filetypes = array('idx','sub','txt','srt');
        $max_filesize = 5242880; //bytes

        $prefix = substr(md5(time()),0,7); //new name of the file
        $target_path[$count] = $destination_path . $prefix .".".$ext[$count];

        // Check if the filetype is allowed, if not DIE and inform the user.
        if(!in_array($ext[$count],$allowed_filetypes)){
            $result[$count] = 2;
            $message[$count] = "The file you attempted to upload is not allowed.".$fileName[$count];}

       // Now check the filesize, if it is too large then DIE and inform the user.
        else if(filesize($_FILES['myfile']['tmp_name'][$count]) > $max_filesize){
            $result[$count] = 3;
            $message[$count] = "The file you attempted to upload is too large.";}

        else if(!file_exists($destination_path)){
            $result[$count] = 4;
            $message[$count] = "The upload path does not exist";}

       // Check if we can upload to the specified path, if not DIE and inform the user.
        else if(!is_writable($destination_path)){
            $result[$count] = 5;
            $message[$count] = "You cannot upload to the specified directory, please CHMOD it to 777.";}

        else 
        {       
            @move_uploaded_file($tmpName[$count], $target_path[$count]);

            $file_info = pathinfo($fileName[$count]);
            $sql = "INSERT INTO Files SET 
                        uploader_ip = '".$_SERVER['REMOTE_ADDR']."',
                        File_Name = '".$fileName[$count]."',
                        File_Type = '".$fileType[$count]."',
                        File_Size = '".$fileSize[$count]."',
                        File_Hash = '".$prefix.".".$ext[$count]."',
                        File_Extension = '".$file_info['extension']."'";

            $sqlresult = mysql_query($sql);    
            // If the query was successful, give success message
            if(!$sqlresult){
                $result[$count] = 6;
                $message[$count] = "Could not add this file.";//not actually displayed
                 exit;
            }
            else{
                $message[$count] =  "New file successfully added.";//not actually displayed
                $result[$count] = 1;
                $path[$count] = 'Your file upload was successful, view the file <a href="' . $target_path[$count] . '" title="Your File">here</a>';
            }

        }//closes last else (all the writing to the db)
    }
    sleep(1);
?>

<script language="javascript" type="text/javascript">window.top.window.stopUpload(
<?php echo json_encode($result[$count]); ?>,
<?php echo json_encode($message[$count]); ?>,
<?php echo json_encode($path[$count]); ?>,
<?php echo json_encode($count); ?>,
<?php echo json_encode($fileName[$count]); ?>,
<?php echo json_encode($ext[$count]); ?>);
</script>   

每次我收到错误“您上传的文件不允许”时,它应该通过该测试。非常感谢任何帮助。

【问题讨论】:

    标签: php mysql ajax file-upload


    【解决方案1】:

    我认为在for 循环内的数组中使用$count 的任何地方,都需要使用$i 而不是$count

    $count 始终相同(并且在数组边界之外)。

    试试看你是否还有运气。

    【讨论】:

    • 天哪,我已经准备好踢自己了。我相当肯定你是对的。我会对其进行测试并回复您。
    • 好的,我通过并使用了$i。使用 $count 肯定是错误的,但现在似乎没有一个变量被初始化。当我只输入一个文件并点击提交时,这是 upload.php 发送到表单的响应:&lt;script language="javascript" type="text/javascript"&gt;window.top.window.stopUpload( null, null, null, 4, null, null); &lt;/script&gt;
    • 由于可能有多个文件,您可能还需要将 stopUpload javascript 调用放入一个循环中。您是将该部分保留为$count 还是将其更改为$i?在循环之外,$i 等于 count。
    • 我把它改成了 $i 并离开了循环。实际上,我现在可能希望那些返回完整数组,因为我考虑过它并让 js 方法处理读取它。但无论哪种方式,现在数组只是空的,它们没有将任何值写入其中。思考为什么?
    • 所以var_dump$fileName 或任何其他值返回null
    猜你喜欢
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2012-10-27
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多