2012-07-08 127 views
0

我正在運行PHP腳本來從HTML表單上傳文件,重命名它們並將它們放在我的服務器上。它是上傳和重命名,但每個文件名現在開頭的單詞「陣列」
即:ArrayTest Document v1.0.pdf
我相當有信心它不是我的$newfn變量,因爲我顯示在一個表中,它不顯示。ARRAY之前上傳後重命名文件名 - PHP

//This gets all the other information from the form 
$name=$_POST['docname']; 
$version=$_POST['docver']; 
$date=$_POST['docdate']; 
$type=$_POST['doctype']; 
$author=$_POST['docauth']; 
$file=$_FILES['uploaded']['name']; 

//target directory is assigned 
$target = $directory; 
$target = $target . basename($file) ; 

//grab file extension 
$filetypes = array(
'image/png' => '.png', 
'image/gif' => '.gif', 
'image/jpeg' => '.jpg', 
'image/bmp' => '.bmp', 
'application/pdf' => '.pdf'); 
$ext = $filetypes[$_FILES['uploaded']['type']]; 

//generate new filename variable "name v??.xxx" 
$newfn = $name . ' ' . 'v' . $version . $ext; 

//Check to see if file exists 
if (file_exists($directory . $file)) 
{ 
echo $_FILES["uploaded"]["name"] . " already exists. "; 
} 
else 
{ 
//if its a new file, change name and upload 
    if (move_uploaded_file($_FILES["uploaded"]["tmp_name"], 
     $directory . $_FILES["uploaded"] . $newfn)) 
     { 
     echo "The file ". basename($file). " has been uploaded"; 
     } 
     else 
     { 
     echo "Sorry, there was a problem uploading your file."; 
     } 
} 

我覺得它與我在move_uploaded_file命令$FILES['uploaded']部分。我嘗試了谷歌搜索,但只要我提到「數組」我的谷歌結果是沒有什麼遠程相同的。

好吧,謝謝下面我解決了它。爲了在以後任何人,我刪除整個$_FILES陣列,使代碼讀取,

//if its a new file, change name and upload 
if (move_uploaded_file($_FILES["uploaded"]["tmp_name"], 
    $directory . $newfn)) 

現在一切都以正確的名稱結構上傳。謝謝。

+1

它showas數組,因爲$ _FILES [「上傳」]是一個數組。 – 2012-07-08 06:33:26

回答

0

應該

move_uploaded_file($_FILES["uploaded"]["tmp_name"],$directory . $_FILES["uploaded"]["name"] . $newfn) 

你忘了,包括[ 「名稱」]後$ _FILES [ 「上傳」]

+0

如果我把'[「名稱」]放在原文件名的前面新名稱 'Original Document.pdf'變成 'Original Document.pdf New Document.pdf' – Hux 2012-07-08 06:56:43

+0

如果你只是想要New Document.pdf ,沒有理由包含$ _FILES [「上傳」] [「名稱」]。只要把它作爲$目錄。 $ newfn – 2012-07-08 07:11:40

0

在您的move_uploaded_file您使用的是全$_FILE['uploaded']陣列作爲名稱,而不只是文件名。一個小小的調整:

if (move_uploaded_file($_FILES["uploaded"]["tmp_name"], 
     $directory . $_FILES["uploaded"]["name"] . $newfn)) 
     { 

而且你應該都很好。