2012-07-05 34 views
0

我想通過PHP上傳文件。我希望這是一個下拉選擇,而不是隻上傳到「upload」目錄。所以基本上,我希望表單有一個下拉菜單和我選擇的任何目錄,它就是這樣。那麼,我怎樣才能讓它有下拉?PHP文件上傳器

這是我的代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<?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 _ inplace of blank space in file name, you can remove this line 

$add = "upload/$filename"; // upload directory path is set 
//echo $_FILES['images']['type'][$key];  // uncomment this line if you want to display the file type 
//echo "<br>";        // Display a line break 
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> 
+2

問題是? – 2012-07-05 00:10:27

+0

您應該爲HTML屬性使用雙引號,而不是單引號。只是想逃脫他們這樣:'\「' – C0deH4cker

回答

3

添加一些HTML類似形式如下:

Upload Directory: 
<select name="uploadDir"> 
    <option value="uploads">Uploads/</option> 
    <option value="uploads2">Uploads2/</option> 
</select> 

通過改變$一些PHP魔術灑它添加到:

$add = "{$_POST['uploadDir']}/$filename"; 

和瞧! :D請務必在目錄上做某種驗證,因爲它非常不安全,但給了你如何去做的概念;)

祝你好運!

+0

現在讓我上傳index.php到網絡路徑... – 2012-07-05 00:15:13

+0

之前或之後,你編輯它?:-) – 2012-07-05 00:32:12