2016-01-13 76 views
-5

我試圖應用一些現有的樣本在這裏找到在stackoverflow但這個問題似乎沒有解決。如何解決爲foreach()php提供的無效參數?

$valid_formats = array("jpg", "png", "gif", "zip", "bmp"); 
$max_file_size = 1024*1000; 
$path = "upload/"; 
$count = 0; 
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){ 

    foreach ($_FILES['files']['name'] as $f => $name) { 
     if ($_FILES['files']['error'][$f] == 4) { 
      continue; // Skip file if any error found} 
      if ($_FILES['files']['error'][$f] == 0) {   
       if ($_FILES['files']['size'][$f] > $max_file_size) { 
        $message[] = "$name is too large!."; 
        continue; // Skip large files 
       } 
       elseif(! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)) { 
     $message[] = "$name is not a valid format"; 
     ... 
+6

'$ _FILES [「文件」] [「名」]'是不是數組。這就是爲什麼你得到這個錯誤 – urfusion

+0

正確解釋你的問題。你有什麼嘗試?什麼是問題? – Ironic

+0

$ _FILES ['files'] ['name']將此代碼替換爲$ _FILES ['files']。 (我想如果你提交多個文件的形式) –

回答

0

正如urfusion所說,$ _FILES ['files'] ['name']是一個字符串,而不是一個數組。

,你可以做遍歷文件提交的所有文件名是類似的東西:

if(is_array($_FILES['files']) && !empty($_FILES['files'])){ // if1 
    foreach($_FILES['files'] as $file_index=>$file_value){ 
    if($file_index==='name'){ // if2 
     echo $file_value; // or do something with that name 
    } // end if2 
    } // end foreach 
} // end if1