2012-04-16 45 views
0

我喜歡用數組的幫助上傳一些圖像到目錄。因此,我有這樣的代碼:多圖像上傳文件上傳錯誤數量

$allowedExtensions = array('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif'); 
      $maxSize = 2097152; 
      $Dir = "a/b/c/d"; 
      $storageDir = "a/b/c/d/tmp_images"; 

      //Arrays 
      $errors2 = $output = array(); 


      if(!empty($_FILES['image'])){ 

      // Validation loop (I prefer for loops for this specific task) 
      for ($i = 0; isset($_FILES['image']['name'][$i]); $i++) { 

       $fileName = $_FILES['image']['name'][$i]; 
       $fileSize = $_FILES['image']['size'][$i]; 
       /*$fileErr = $_FILES['image']['error'][$i];*/ 
       $fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); 

       // Dateiendung überprüfen 
       if (!in_array($fileExt, $allowedExtensions)) { 
        $errors2[$fileName][] = "format $fileExt in $fileName is not accepted"; 
       } 

       // check filesize 
       if ($fileSize > $maxSize) { 
        $errors2[$fileName][] = "maxsize of 2MB exceeded"; 
       } 


      } 

      /*// Handle validation errors here 
      if (count($errors) > 0) { 
       echo "Fehler beim Upload des Bildmaterials:"; 
       echo ($errors); ->gibt "Array" aus 
      }*/ 


      if (is_dir($Dir)){ 
       mkdir($storageDir, 0755); 
      }else{ 
       mkdir($Dir, 0755); 
       mkdir($storageDir, 0755); 
      } 


      // Fileupload 
      for ($i = 0; isset($_FILES['image']['name'][$i]); $i++) { 

      // Get base info 
      $fileBase = basename($_FILES['image']['name'][$i]); 
      $fileName = pathinfo($fileBase, PATHINFO_FILENAME); 
      $fileExt = pathinfo($fileBase, PATHINFO_EXTENSION); 
      $fileTmp = $_FILES['image']['tmp_name'][$i]; 

      // Construct destination path 
      $fileDst = $storageDir.'/'.basename($_FILES['image']['name'][$i]); 
      for ($j = 0; file_exists($fileDst); $j++) { 
       $fileDst = "$storageDir/$fileName-$j.$fileExt"; 
      } 

      // Move the file 

      if (count($errors2) == 0) { 
       if (move_uploaded_file($fileTmp, $fileDst)) { 
            ... 
           } 
         } 

與代碼的問題是以下內容:在上載與所接受結束它將回聲出兩個或多個文件的情況下:

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to access abc2.png in /a/b/xxx.php on line xxx 

這指的是線:

if (move_uploaded_file($fileTmp, $fileDst)) { 

此消息將顯示除第一個以外的每張圖片。所以我不知道我做錯了什麼。如果有人能夠幫助我,我會非常感激。我真的很感激。非常感謝。

回答

1

你的代碼是非常similar我的答案在limiting the checking condition while uploading swf files

這是你應該如何實現這樣的..

完整劇本

<?php 
error_reporting (E_ALL); 
$allowedExtensions = array (
     'jpg', 
     'jpeg', 
     'png', 
     'bmp', 
     'tiff', 
     'gif' 
); 
$maxSize = 2097152; 
$dirImage = "photos/tmp_images"; 
$errors = $output = array(); 
if (isset ($_FILES ['image'])) { 
    foreach ($_FILES ['image'] ['tmp_name'] as $key => $val) { 

     $fileName = $_FILES ['image'] ['name'] [$key]; 
     $fileSize = $_FILES ['image'] ['size'] [$key]; 
     $fileTemp = $_FILES ['image'] ['tmp_name'] [$key]; 

     $fileExt = pathinfo ($fileName, PATHINFO_EXTENSION); 
     $fileExt = strtolower ($fileExt); 

     if (empty ($fileName)) 
      continue; 

      // Dateiendung überprüfen 
     if (! in_array ($fileExt, $allowedExtensions)) { 
      $errors [$fileName] [] = "format $fileExt in $fileName is not accepted"; 
     } 

     if ($fileSize > $maxSize) { 
      $errors [$fileName] [] = "maxsize of 2MB exceeded"; 
     } 

     if (! mkdir_recursive ($dirImage, 0777)) { 
      $errors [$fileName] [] = "Error Creating /Writing Directory $dirImage "; 
     } 

     // Construct destination path 
     $fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName; 
     $filePrifix = basename ($fileName, "." . $fileExt); 
     $i = 0; 
     while (file_exists ($fileDst)) { 
      $i ++; 
      $fileDst = $dirImage . DIRECTORY_SEPARATOR . $filePrifix . "_" . $i . "." . $fileExt; 

     } 
     // Move the file 

     if (count ($errors) == 0) { 
      if (move_uploaded_file ($fileTemp, $fileDst)) { 
       // ... 

       $output [$fileName] = "OK"; 
      } 
     } 

    } 
} 

function mkdir_recursive($pathname, $mode) { 
    is_dir (dirname ($pathname)) || mkdir_recursive (dirname ($pathname), $mode); 
    return is_dir ($pathname) || mkdir ($pathname, $mode); 
} 
if (! empty ($errors)) { 
    echo "<pre>"; 
    foreach ($errors as $file => $error) { 
     echo $file, PHP_EOL; 
     echo "==============", PHP_EOL; 
     foreach ($error as $line) { 
      echo $line, PHP_EOL; 
     } 
     echo PHP_EOL; 
    } 
    echo "</pre>"; 
} 

if (! empty ($output)) { 
    echo "<pre>"; 
    echo "Uploaded Files", PHP_EOL; 
    foreach ($output as $file => $status) { 
     echo $file, "=", $status, PHP_EOL; 
    } 

    echo "</pre>"; 
} 
?> 


<form method="post" enctype="multipart/form-data"> 
    <label for="file">Filename 1:</label> <input type="file" name="image[]" 
     id="file" /> <br /> <label for="file">Filename 2:</label> <input 
     type="file" name="image[]" id="file" /> <br /> <label for="file">Filename 
     3:</label> <input type="file" name="image[]" id="file" /> <br /> <input 
     type="submit" name="submit" value="Submit" /> 
</form> 
+0

所有你需要做的是更新問題,而不是創建另一個... @bonny – Baba 2012-04-17 09:17:52

+0

它沒有增長,因爲你接受了一個答案,但沒有表明你仍然有問題...當你編輯和添加新的信息時,每個人都會意識到並看到他們可以做些什麼,直到問題得到解決...... – Baba 2012-04-17 09:26:29

+0

該功能假設是在同一頁.......我會編輯它所以你可以理解如何使用它... – Baba 2012-04-17 09:39:51

2

首先,我將命名上傳的字段分隔。例如。命名第一個字段<input name="image_1" type="file" />和第二個<input name="image_2" type="file" />。然後你可以遍歷數組$_FILES改爲:

foreach($_FILES as $fileId => $file){ 
    //make sure it's a file you want (optional) 
    if(!preg_match("/^image\_\d+$/",$fileId){ 
     continue; 
    } 

    //the rest of your code from the for loop 
} 

其次,你需要確保你的表單的加密類型是multipart/form-data

這有幫助嗎?

+0

他用HTML5' ',所以他以這種形式得到它們在。 – 2012-04-16 08:08:09

+0

你好,謝謝或回答。問題是我不能輕易地命名每個字段,因爲這個輸入字段是由一個函數創建的。 – bonny 2012-04-16 09:22:01

1

你爲什麼要訪問$_FILES超全局數組作爲三維數組?

如果您想從<input type="file" name="image"/>上傳文件的文件名,您所要做的只是$name = $_FILES[ 'image' ][ 'name' ]最後不需要[ $i ]

您需要循環像這樣在$ _FILES中的條目:

foreach ($_FILES as $inputName => $fileData) 
{ 
    // $inputName is be 'image` for the first input and so on, 
    // $fileData will be an array of the attributes [ 'name', 'tmp_name', ... ] 
} 
+1

他使用HTML5'',所以他以這種格式獲取它們。 – 2012-04-16 08:08:28

+0

我使用它,因爲我想顯示每個文件的顯式錯誤消息。 – bonny 2012-04-16 09:23:37

1

我不知道這是否可以爲您解決,但你可以嘗試將這些轉換爲「正常」 $_FILES值。

$arr_files = @$_FILES['image']; 

$_FILES  = array(); 
foreach(array_keys($arr_files['name']) as $h) 
$_FILES["image_{$h}"] = array( 'name'  => $arr_files['name'][$h], 
            'type'  => $arr_files['type'][$h], 
            'tmp_name' => $arr_files['tmp_name'][$h], 
            'error'  => $arr_files['error'][$h], 
            'size'  => $arr_files['size'][$h]); 

然後像正常一樣運行循環。

See previous related answer