2012-03-17 60 views
0

我正在寫一個相當簡單的文件服務器,並且上傳單個文件,將其移動到服務器上的一個文件夾並保存它在數據庫中的相關信息做得很好。現在,當我嘗試修改它以接受來自單個輸入字段的多個文件時,我無法通過第一次測試找出錯誤。問題上傳多個文件到服務器

這是我的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> 

每當我收到錯誤「您上傳的文件是不允許的」時,它應該通過那個測試。任何幫助是極大的讚賞。

回答

0

我認爲無論你在for循環中的數組中使用$count,你需要使用$i而不是$count

$count總是相同的(並且在數組邊界之外)。

嘗試一下,看看你是否有更多的運氣。

+0

哦,我的主人,我準備踢自己。我相當肯定你是對的。我會測試它並回復你。 – 2012-03-17 18:05:01

+0

好的,我經歷了$ i。使用$ count絕對是錯誤的,但現在沒有一個變量似乎被初始化。當我只輸入一個文件並點擊提交時,這是upload.php發送到表單的響應: '' – 2012-03-17 18:09:27

+0

由於可能有多個文件,因此您可能還需要將'stopUpload' javascript調用放入循環中。你把這部分作爲'$ count'還是把它改爲'$ i'?在循環之外,「$ i」等於count。 – drew010 2012-03-17 18:17:06