2013-05-07 53 views
0

我使用while循環上傳多個文件,下面是我的代碼,move_uploaded_file總是失敗,我得到的消息「上傳文件時出錯!」。我使用相同的代碼(while循環中沒有$ counter的代碼)在其他用例中上傳單個文件,這很好,但是當我在while循環中使用相同的代碼時,我認爲代碼看起來很好,但在move_uploaded_file ...請告訴我最新的問題,我該如何解決這個問題?move_uploaded_file未能支持多種文件上傳while循環

$counter = 0; 
while ($_FILES) { 

if (!isset($_FILES['album_pics'])) { 
    //required variables are empty 
    die("File is empty!"); 
} 
if ($_FILES['album_pics']['error'][$counter]) { 
    //File upload error encountered 
    die(upload_errors($_FILES['album_pics']['error'][$counter])); 
} 

$FileName = strtolower($_FILES['album_pics']['name'][$counter]); //uploaded file name 
$ImageExt = substr($FileName, strrpos($FileName, '.')); //file extension 
$FileType = $_FILES['album_pics']['type'][$counter]; //file type 
$FileSize = $_FILES['album_pics']["size"][$counter]; //file size 
$RandNumber = rand(0, 9999999999); //Random number to make each filename unique. 
$uploaded_date = date("Y-m-d H:i:s"); 
$FileTitle = current(explode('.', $FileName)); 

switch (strtolower($FileType)) { 
    case 'image/png': 
    case 'image/gif': 
    case 'image/jpeg': 
    case 'application/pdf': 
    case 'application/msword': 
    case 'application/vnd.ms-excel': 
    case 'application/x-zip-compressed': 
    case 'text/plain': 
    case 'text/html': 
     break; 
    default: 
     die('Unsupported File!'); //output error 
} 

//File Title will be used as new File name 
$NewFileName = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), strtolower(substr($FileTitle, 0, 10))); 
$NewFileName = $NewFileName.'_'.$RandNumber.$ImageExt; 
$UploadDirectory = 'ajax/'; 


if (move_uploaded_file($_FILES['album_pics']['tmp_name'][$counter], $UploadDirectory.$NewFileName)) { 
    // here I will insert image metadata to database with the new image name 

    $counter = (if result is true) ? $counter + 1 : $counter;// increase counter if insert returns true 
} else { 
    die('error uploading File!'); 
} 

function upload_errors($err_code) { 
    switch ($err_code) { 
     case UPLOAD_ERR_INI_SIZE: 
      return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'.ini_get("upload_max_filesize"); 
     case UPLOAD_ERR_FORM_SIZE: 
      return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; 
     case UPLOAD_ERR_PARTIAL: 
      return 'The uploaded file was only partially uploaded'; 
     case UPLOAD_ERR_NO_FILE: 
      return 'No file was uploaded'; 
     case UPLOAD_ERR_NO_TMP_DIR: 
      return 'Missing a temporary folder'; 
     case UPLOAD_ERR_CANT_WRITE: 
      return 'Failed to write file to disk'; 
     case UPLOAD_ERR_EXTENSION: 
      return 'File upload stopped by extension'; 
     default: 
      return 'Unknown upload error'; 
    } 
} //function upload_erros() close 
} //while loop close 
+0

似乎它可能是一個目錄權限問題。另外,你是否得到這一行的文件大小? $ FileSize = $ _FILES ['album_pics'] [「size」] [$ counter]; //文件大小 – 2013-05-07 10:46:18

+0

我不認爲它的權限問題,我上傳與相同的代碼和相同的目錄單文件,是我得到的文件大小 – 2013-05-07 10:48:09

+0

$計數器=(如果結果爲真)? $ counter + 1:$ counter; //首先初始化「result」var。否則,你的代碼是好的 – 2013-05-07 10:51:33

回答

0

我已經找到了答案,我張貼這使任何人誰想要解決多文件上傳可以參考而不是使用這個

while循環我改成了foreach循環這樣

foreach($_FILES['album_pics']['error'] as $key => $error){ 
    all code remains the same as above in while loop, except every reference to $counter will be replaced by $key 
} 
0

最初重新排列文件以更好地循環它們。

https://gist.github.com/lukeoliff/5531772

可能使這種功能的基礎上更容易割肉出局。

<?php 
    if (!empty($_FILES)) { 
    $_FILES = rearrangeFiles($_FILES); 
    } 
?> 

打開以下...

Array 
(
    [name] => Array 
     (
      [0] => foo.txt 
      [1] => bar.txt 
     ) 

    [type] => Array 
     (
      [0] => text/plain 
      [1] => text/plain 
     ) 

    [tmp_name] => Array 
     (
      [0] => /tmp/phpYzdqkD 
      [1] => /tmp/phpeEwEWG 
     ) 

    [error] => Array 
     (
      [0] => 0 
      [1] => 0 
     ) 

    [size] => Array 
     (
      [0] => 123 
      [1] => 456 
     ) 
) 

成....

Array 
(
    [0] => Array 
     (
      [name] => foo.txt 
      [type] => text/plain 
      [tmp_name] => /tmp/phpYzdqkD 
      [error] => 0 
      [size] => 123 
     ) 

    [1] => Array 
     (
      [name] => bar.txt 
      [type] => text/plain 
      [tmp_name] => /tmp/phpeEwEWG 
      [error] => 0 
      [size] => 456 
     ) 
) 
+0

重新排列什麼和它的用途是什麼? – 2013-05-07 10:51:32

+0

我將編輯我的回答對使用:) – 2013-05-07 10:56:19

+0

OK讓我試試這個 – 2013-05-07 11:01:31

0

請移動功能,同時循環之外..然後再次運行。

+0

我沒有得到,你通過移動功能外,同時循環 – 2013-05-07 11:06:10

+0

我的意思是你的每次循環運行,該功能一次又一次地宣佈自己的意思。它不應該是這樣的。 – 2013-05-07 11:09:43

+0

我不明白你在說... – 2013-05-07 11:15:21