2013-11-26 70 views
1

我試圖上傳多個文件到服務器,同時向數據庫中輸入上傳信息。出於某種原因,只有第一個或最後一個文件被放入數據庫,但我不能爲我的生活找出原因!通過PHP上傳多個文件並將信息發佈到MYSQL數據庫

任何幫助表示讚賞 - 謝謝!

public function processNewUploads() 
{ 
    $language = OW::getLanguage(); 
    $osuploadBOL = OSUPLOAD_BOL_Service::getInstance(); 

    //Loop through each file that was uploaded 
    for($i=0; $i < count($_FILES['uploads']['tmp_name']); $i++){ 


     /* check if there is a file in the array 
for($i=0; $i < count($_FILES['uploads']['tmp_name']); $i++){    
     if(!is_uploaded_file($_FILES['uploads']['tmp_name'][$i])) 
     { 
      OW::getFeedback()->error($language->text('osupload','no_files_selected')); 
      OW::getApplication()->redirect(OW::getRouter()->urlForRoute('base_index')); 
     } */ 


     //Check to see if there was an error 
     if($_FILES['uploads']['error'][$i] > 0){ 
      $error = $_FILES['uploads']['error'][$i]; 
     }else{ 
      $error = 0; 
     } 

     //Prepare information to enter into database// 

     //If the user is logged in then get the userId 
     if(OW::getUser()->isAuthenticated()) { 
      $fileOwner = OW::getUser()->getId(); 
     } else { 
      $fileOwner = 0; 
     } 

     //Get the IP of the uploader 
     $fileOwnerIp = $_SERVER['REMOTE_ADDR']; 

     //Get the raw file name 
     $rawFileName = $_FILES['uploads']['name'][$i]; 

     //Get the unique file name 
     $uniqueFileName = uniqid('',true); 

     //Get the upload time 
     $uploadTimeStamp = time(); 

     //Get the file extension 
     $fileExtension = explode(".", $_FILES['uploads']['name'][$i]); 
     $n = count($fileExtension) - 1; 
     $fileExtension = '.'.strtolower($fileExtension[$n]); 

     //Get the size of the file 
     $fileSize = $_FILES['uploads']['size'][$i]; 

     //Get the display name of the file 
     $fileDisplayName = $rawFileName; 

     //Insert the file information into the database 
     $sql = 'INSERT INTO ' . OSUPLOAD_BOL_OsuploadDao::getInstance()->getTableName() . " (fileOwner,fileOwnerIp,rawFileName,uniqueFileName,uploadTimeStamp,fileExtension,fileSize,fileDisplayName,error) 
     VALUES ('$fileOwner','$fileOwnerIp','$rawFileName','$uniqueFileName.$fileExtension','$uploadTimeStamp','$fileExtension','$fileSize','$fileDisplayName','$error')"; 

     OW::getDbo()->Insert($sql); 

     if(!$error > 0){ 
      //Move the new upload as long as there was not an error with it 
      $fileToMove = $_FILES['uploads']['tmp_name'][$i]; 
      $osuploadBOL->moveNewUpload($fileToMove,$uniqueFileName,$fileExtension); 
      continue; 
     }else{ 
      //If there was an error just go to the next file 
      continue; 
     } 
    } 
} 

} 

HTML:

<input name="uploads[]" id="uploads" type="file" multiple=""> 
+0

上傳表單上的每個文件中的元素都必須有一個唯一的名稱(或者是一個數組),否則'$ _FILES'數組中的鍵將被覆蓋與最後一個文件。您可能只需將'[]'添加到名稱的末尾,然後循環顯示名稱下的所有內容。 –

+0

感謝您的回覆!我不確定你的意思到底是什麼 - 我想如果你上傳多個文件,他們會在數組中?如果不是,我會如何將它們放入數組中? – RuFFCuT

+0

@JonathanKuhn得到的是''name屬性,所以'$ _FILES ['uploads']'是一個文件的名稱,並以html的形式表示爲'否則使用像''這樣的數組,'請注意將'uploads []'作爲數組。之後,執行'var_dump($ _ FILES)'來查看結構 – gwillie

回答

0

這只是你的一個快速上擡,有可能是錯誤的。

使用foreach,而不是我想:

public function processNewUploads() 
{ 
    $language = OW::getLanguage(); 
    $osuploadBOL = OSUPLOAD_BOL_Service::getInstance(); 

    //Loop through each file that was uploaded 
    foreach($_FILES as $key => $file){ 

     $error = $file['error'] ? 
     /* 
     if($file['error']){ 
      $error = $file['error']; 
     }else{ 
      $error = 0; 
     } 
     */ 

     //If the user is logged in then get the userId 
     if(OW::getUser()->isAuthenticated()) { 
      $fileOwner = OW::getUser()->getId(); 
     } else { 
      $fileOwner = 0; 
     } 

     //Get the IP of the uploader 
     $fileOwnerIp = $_SERVER['REMOTE_ADDR']; 

     //Get the raw file name 
     $rawFileName = $file['name']; 

     //Get the unique file name 
     $uniqueFileName = uniqid('',true); 

     //Get the upload time 
     $uploadTimeStamp = time(); 

     //Get the file extension 
     $fileExtension = explode(".", $file['name']); 
     $n = count($fileExtension) - 1; 
     $fileExtension = '.'.strtolower($fileExtension[$n]); 

     //Get the size of the file 
     $fileSize = $file['size']; 

     //Get the display name of the file 
     $fileDisplayName = $rawFileName; 

     //Insert the file information into the database 
     $sql = 'INSERT INTO ' . OSUPLOAD_BOL_OsuploadDao::getInstance()->getTableName() . " (fileOwner,fileOwnerIp,rawFileName,uniqueFileName,uploadTimeStamp,fileExtension,fileSize,fileDisplayName,error) 
     VALUES ('$fileOwner','$fileOwnerIp','$rawFileName','$uniqueFileName.$fileExtension','$uploadTimeStamp','$fileExtension','$fileSize','$fileDisplayName','$error')"; 

     OW::getDbo()->Insert($sql); 

     if(!$error > 0){ 
      //Move the new upload as long as there was not an error with it 
      $fileToMove = $file['tmp_name']; 
      $osuploadBOL->moveNewUpload($fileToMove,$uniqueFileName,$fileExtension); 
      continue; 
     }else{ 
      //If there was an error just go to the next file 
      continue; 
     } 
    } 
} 
相關問題