2012-07-19 110 views
0

我試圖編碼一個腳本,使其可以上傳多個圖像。這是我正在使用的代碼。PHP多上傳圖片問題

if(isset($_POST['submit_images'])) { 
    $i = 0; 
    foreach($_FILES['file'] as $file) { 
     $image = new Image(); 
     $image->member_id = $_POST['id'][$i]; 
     $image->image_type = "MedlemsBillede"; 
     $image->attach_file($_FILES['file']); 
     if($image->save()) { 
      $message = 'Billedet blev uploadet med succes.'; 
     } else { 
      $message = join("<br />", $image->errors); 
     } 
     $i++; 
    } 
} 

我的問題就出在這調用函數:

$image->attach_file($_FILES['file']); 

而且該功能看起來像這樣:

public function attach_file($file) { 
    if(!$file || empty($file) || !is_array($file)) { 
     $this->errors[] = "Der blev ikke uploadet nogen fil."; 
     return false; 
    } elseif($file['error'] != 0) { 
     $this->errors[] = $this->upload_errors[$file['error']]; 
     return false; 
    } else { 
     $this->temp_path = $file['tmp_name']; 
     $this->file_name = str_replace(' ', '_', basename($file['name'])); 
     $this->file_type = $file['type']; 
     $this->file_size = $file['size']; 
     $this->ts = date('Y-m-d H:i:s'); 
     return true; 
    } 
} 

是否有人對如何解決這一問題的想法?

+0

*我的問題在於這個函數的調用* ...有什麼問題?任何錯誤?它執行時會發生什麼? – ManseUK 2012-07-19 15:54:21

+0

我無法發送到通過attach_file函數更正數組的一部分。當我僅使用該功能上傳一個文件時,它工作正常。 – nickifrandsen 2012-07-19 16:00:11

回答

0

我認爲這個問題是在這一行:

$image->attach_file($_FILES['file']); 

你迭代$ _FILES [「文件」],而不是僅僅$ _FILES

而且你調用數組,通過它你內循環的迭代

通話應該是這樣的

$image->attach_file($file); 

這可以幫助你

+0

是的我也試過,但是這給了我「未定義索引」錯誤在以下內容:錯誤,tmp_name,名稱,類型,大小 – nickifrandsen 2012-07-19 16:09:33

+0

你一些調試技巧,如嘗試做這個print_r($ _ FILES)只是爲了檢查什麼值數組包含 – rockstar 2012-07-19 16:15:25

+0

Array([file] => Array([name] => Array([0] => cup32.png [1] => cardgame32.png [2] => boxdownload32.png)[type] => Array ([0] => image/png [1] => image/png [2] => image/png)[tmp_name] => Array([0] =>/Applications/MAMP/tmp/php/phpdkIp2J [1 ] =>/Applications/MAMP/tmp/php/phpKwtbda [2] =>/Applications/MAMP/tmp/php/phpquOEWC)[error] => Array([0] => 0 [1] => 0 [2 ] => 0)[size] => Array([0] => 433 [1] => 696 [2] => 1035))) – nickifrandsen 2012-07-19 16:19:04