简体   繁体   中英

Issue uploading multiple files to server

I'm writing a fairly simple fileserver and was doing well with uploading a single file, moving it to a folder on the server and keeping info about it in a database. Now when I tried modifying it to accept multiple files from a single input field, I can't get it to progress past the first test for errors.

This is my index.php here:

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

And my upload.php here:

    <?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>   

Every time I am getting the error "the file you uploaded is not allowed" when it should pass that test. Any help is greatly appreciated.

I think that everywhere that you are using $count in the arrays inside your for loop, you need to be using $i instead of $count .

$count is always the same (and is outside the bounds of the array).

Try that and see if you have any more luck.

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