2012-03-10 76 views
0

我有下面的工作腳本,而不是圖像1,圖像2,圖像3,圖像4旁邊的框我想爲每個不同的名稱。是否有捷徑可尋? 在此先感謝! 另外我會把 標題(「Location:thankyou.php」);
exit(); 因爲把這個在之後的那一刻,它只是直接引導到thankyou.php不低於頁面(document.php)添加多個文件PHP

<?php 
$max_no_img = 4; // Maximum number of images value to be set here 
echo "<form method=post action='' enctype='multipart/form-data'>"; 
echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>"; 
for ($i = 1;$i <= $max_no_img;$i++) { 
    echo "<tr><td>Images $i</td><td> 
<input type=file name='images[]' class='bginput'></td></tr>"; 
} 
echo "<tr><td colspan=2 align=center><input type=submit value='Add Image'></td></tr>"; 
echo "</form> </table>"; 
while (list($key, $value) = each($_FILES['images']['name'])) { 
    //echo $key; 
    //echo "<br>"; 
    //echo $value; 
    //echo "<br>"; 
    if (!empty($value)) { // this will check if any blank field is entered 
     $filename = rand(1, 100000) . $value; // filename stores the value 
     $filename = str_replace(" ", "_", $filename); 
     $add = "upload/$filename"; // upload directory path is set 
     copy($_FILES['images']['tmp_name'][$key], $add); 
     echo $add; 
     // upload the file to the server 
     chmod("$add", 0777); // set permission to the file. 

    } 
} 
?> 
</body> 
</html> 
+4

你知道PHP嗎? – 2012-03-10 13:39:21

+0

我是一個newby!試圖學習。 – user1257518 2012-03-10 13:42:20

回答

1

這一行後:

$max_no_img = 4; 

定義一個數組你願意的名字來給每個圖像:

$imgs_names = array('name for first image' , 'name for second image' , 'name for third image'); //and so on... 

,而不是和:

echo "<tr><td>Images $i</td><td> 

寫:

echo "<tr><td>Images ".$imgs_names[$i-1]."</td><td> 

關於使用header,還有既然你已經使用echo問題。 在文件的開頭添加ob_start(),在文件的末尾添加ob_flush(),現在您可以在發送輸出後添加header()。

EDIT2:關於你的評論,還有重定向的替代方法。

地址:

$submit = true; 

後:

chmod("$add", 0777); // set permission to the file. 

而經過:

} 
} 

地址:

if(isset($submit) && $submit) 
{ 
echo '<meta http-equiv="refresh" content="0; url=http://www.yoursite.com/thankyou.php">'; 
}