2011-03-25 35 views
1

確定建立一個php webform需要其中一個條目是寫入的文件名。Php上傳表單userinput =文件名

我該如何做到這一點?

這裏是我的PHP代碼...

$filename = "output.txt"; #Must CHMOD to 666 
$text = $_POST['bin']; 
$text2 = $_POST['pcn']; 
$text3 = $_POST['gnum']; 
$text4 = $_POST['memid']; 
$text5 = $_POST['urlen']; 
$text6 = $_POST['urles']; 
$text7 = $_POST['tlogo']; 

# Form must use POST. if it uses GET, use the line below: 
#$text = $_GET['theformfieldname']; #POST is the preferred method 

$fp = fopen ($filename, "w"); # w = write to the file only, create file if it does not exist, discard existing contents 
if ($fp) { 
fwrite ($fp, "$text\r\n"); 
fwrite ($fp, "$text2\r\n"); 
fwrite ($fp, "$text3\r\n"); 
fwrite ($fp, "$text4\r\n"); 
fwrite ($fp, "$text5\r\n"); 
fwrite ($fp, "$text6\r\n"); 
fwrite ($fp, "$text7\r\n"); 
fclose ($fp); 

header("Location: logo.html"); 
} 
else { 
echo ("There was an error please submit your request again"); 
} 

?> 

確定需要$文件名= 「output.txt的」; 來自輸入$ text3 = $ _POST ['gnum']; 。

沿的線的東西:

$文件名= $ _POST [ 'gnum'] TXT; 但是這不會工作..

在此先感謝, 喬

回答

0

,而不是

$filename = $_POST['gnum'].txt; 

你應該使用

$filename = $_POST['gnum'].".txt"; 
+0

Psst your code is broken。 :) – 2011-03-25 20:41:30

+0

固定;)我確實想念'。'在'.txt'中,我做到了! – Nanne 2011-03-25 20:45:57

+0

警告:fopen(rxc45632.txt)[function.fopen]:未能打開流:第15行的/var/www/test/form/welcome.php中的權限被拒絕 有錯誤請重新提交您的請求 – jmituzas 2011-03-25 20:47:56

0

我不超級清楚什麼你在問,但我想你想知道如何將'.txt'添加到$ _GET變量的末尾?

如果是這樣的話,你只需要做到這一點:

$text3 = $_POST['gnum'] . '.txt'; 
0

使用此得到該文件的名稱。

$_FILES["file"]["name"] 
+0

你能解釋一點嗎? – jmituzas 2011-03-26 01:50:41