2017-08-26 127 views
-1

腳本工作,但只發送一個附件。我似乎無法弄清楚我做錯了什麼。它會發送第一附着$ userfile的[ '名稱'] [0],而不是userfile的$ [ '名'] [1]發送多個附件PHP imap只發送1個附件

$i = 0; 
    foreach($_FILES as $userfile){ 
     $tmp_name = $userfile['tmp_name'][$i]; 
     $type = $userfile['type'][$i]; 
     $name = $userfile['name'][$i]; 
     $size = $userfile['size'][$i]; 
      $i++; 
} 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
    enctype="multipart/form-data" name="form1"> 
<input type="file" name="file[]" multiple /> 

原來的代碼是從這裏:How to attach two or multiple files and send mail in PHP

+0

這不是附件發送代碼。如果您需要任何有用的幫助,請提供完整的代碼 –

+0

您只是想以錯誤的方式獲取文件。看到我的答案。您應該在其中指定文件數組名稱。 –

回答

1

更改文件名到userfile的[],那麼應該是這樣的:

應該是這樣的:

$count_uploaded_files = count($_FILES['userfile']['name']); 

for($i = 0; $i < $count_uploaded_files; $i++) 
{ 
    $tmp_name = $_FILES['userfile']['tmp_name'][$i]; 
    $type  = $_FILES['userfile']['type'][$i]; 
    $name  = $_FILES['userfile']['name'][$i]; 
    $size  = $_FILES['userfile']['size'][$i]; 

    // Do whatever you want with $temp_name, $type, $name, and $size ... 
} 

但是如果你想留在文件[]。那麼像這樣:

$count_uploaded_files = count($_FILES['file']['name']); 

for($i = 0; $i < $count_uploaded_files; $i++) 
{ 
    $tmp_name = $_FILES['file']['tmp_name'][$i]; 
    $type  = $_FILES['file']['type'][$i]; 
    $name  = $_FILES['file']['name'][$i]; 
    $size  = $_FILES['file']['size'][$i]; 

    // Do whatever you want with $temp_name, $type, $name, and $size ... 
} 
+0

第二個選項有效。謝謝。 – user1282355