2014-11-04 96 views
1

用戶完成上傳多個文件後,我會顯示屏幕上傳的文件。我也試圖在電子郵件的正文中包含相同的列表。絕對一切正常,包括在屏幕上顯示每個文件,除了$ message_body只列出陣列中的最後一個文件名。有任何想法嗎??

<!DOCTYPE html> 
<html> 
<head> 
<title>upload</title> 
</head> 
<body> 
<?php 

if(isset($_POST['ship-it']) AND $_POST['ship-it'] === "yes") { 
    $yesupload = $_POST['ship-it']; 
    preg_match("/yes/", "".$yesupload.""); 

    for($i=0; $i<count($_FILES['tray']['name']); $i++) { 
     $filename = $_FILES['tray']['name'][$i]; 
     $s_name = $_FILES['tray']['tmp_name'][$i]; 
     move_uploaded_file($s_name, 'https://example.com/inbox/'.$filename.''); 
    } 
    $info = $_SERVER['HTTP_USER_AGENT']; 
    $message_body = "".$_POST["email"]." uploaded the file: ".$_FILES['tray']['name']." 
    https://example.com/inbox/".rawurlencode($filename)." 

    User Agent: ".$_POST["id"]." ".$info.""; 

    $mail_options = [ 
     'sender' => '[email protected]', 
     'to' => '[email protected]', 
     'subject' => 'new file sent', 
     'textBody' => $message_body 
    ]; 

    try { 
     $message = new Message($mail_options); 
     $message->send(); 
    } catch (InvalidArgumentException $e) { 
     echo 'error: '; 
    } 
    echo "<p>All done. Here's what you sent:</p>"; 
    for($i = 0; $i < count($_FILES['tray']['name']); $i++) 
    { 
     echo "<ul style='list-style-type:square'> 
      <li>{$_FILES['tray']['name'][$i]}</li> 
      </ul>"; 
    } 
} 
?> 
<form action="<?php echo $upload_url?>" method="post" enctype="multipart/form-data"> 
    <p>Files to upload: </p> <br> 
    <input type="hidden" name="ship-it" value="yes"> 
    <input type="hidden" name="id" value="<?php echo $_SERVER['REMOTE_ADDR']?>"> 
    <input type="email" name="email" required="" autofocus placeholder="Your email address"><br><br> 
    <input type="file" multiple name="tray[]" required=""> 
    <input type="submit" value="Upload"><br><br> 
</form> 
</body> 
</html> 
+0

您遍歷'$ _FILES [「托盤」] [「名」]'當你告訴你在屏幕上的消息,但你不這樣做,當你建立你的郵件正文。 – jeroen 2014-11-04 21:00:40

+1

1.似乎_https:// url不能在'move_uploaded_file'中。 2. preg_match的調用在那裏不清楚。 – Deadooshka 2014-11-04 21:36:05

+0

^^^確切^^使用服務器路徑而不是URL。 – 2014-11-04 21:42:53

回答

1

你已經擁有一個顯示網頁上的文件名的循環。您需要一個類似的循環來將文件名存儲在$message_body中。您可以將其合併到您已經用於移動上傳文件的循環中。也許是這樣的:

$message_body = "".$_POST["email"]." uploaded these files:\n"; 

for($i=0; $i<count($_FILES['tray']['name']); $i++) { 

    $filename = $_FILES['tray']['name'][$i]; 
    $s_name = $_FILES['tray']['tmp_name'][$i]; 
    move_uploaded_file($s_name, 'https://example.com/inbox/'.$filename.''); 

    $message_body .= "* " . $_FILES['tray']['name'][$i] 
       . "-> https://example.com/inbox/" . rawurlencode($filename) 
       . "\n"; 
} 
0

您之前的代碼似乎將$_FILES['tray']['name']視爲一個數組。您是否嘗試過它的內爆,用逗號,從數組中把它轉換爲字符串:

$message_body = "".$_POST["email"]." uploaded the file: ".implode(", ",$_FILES['tray']['name'])."https://example.com/inbox/".rawurlencode($filename)." 
+0

謝謝,Mainstreetmark。下次我會試試這個。 – Dan 2014-11-04 22:16:38

相關問題