2013-04-22 37 views
1

我正在向我的網站添加一個文件上傳系統,我似乎無法獲得與生成的文件名匹配的文件鏈接。我將一個時間函數添加到文件名的前面,使其具有唯一性(它會根據時間生成一個數字)。它確實這樣做了,但由於某種原因,該唯一名稱未保存在數據庫中。有人能幫我解決這個問題嗎?添加時間到上傳的文件和數據庫

//This is the directory where images will be saved 
$target = "files/"; 
$target = $target. time(). basename($_FILES['photo']['name']); 
echo $target; 


//postThis gets all the other information from the form 
$tfid=$_POST['tfid']; 
$fname=$_POST['fname']; 
$lname=$_POST['lname']; 
$hca=$_POST['hca']; 
$file=($_FILES['photo']['name']); 


//Writes the information to the pic table 
mysql_query("INSERT INTO pic 
(tfid, file) 
VALUES ('$tfid', '$file')") 
or die(mysql_error()); 
ECHO "<strong>pic table has been saved.<br></strong>"; 


//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "<strong>The file has been uploaded</strong>"; 
} 
else { 

    //Gives an error if its not 
echo "<strong>Sorry, there was a problem uploading your file.</strong>"; 
} 
echo '<br>'; 
require 'insertbuttons.php'; 
+0

你在這裏寫的是什麼增加了上傳文件時的時間?它看起來不會那樣做。 – 2013-04-22 21:05:40

+0

更像是它在它的正面添加了一個隨機生成的數字。我將編輯我的帖子以反映這一點。 – kayle 2013-04-22 21:08:18

+0

您在這裏爲'$ target'變量的代碼看起來不正確。它看起來會隨着時間的推移將您的文件移動到一個文件夾。 – 2013-04-22 21:09:49

回答

0

我明白現在發生了什麼。對評論感到抱歉。您需要將$target變量保存在數據庫中。發佈的變量與目標不匹配。因此,在SQL語句中有$file的位置,您可能需要$target。這應該給你的文件的名稱,但沒有擴展它看起來像。

+0

非常感謝。那就是訣竅。我也意識到我的鏈接中嵌入了第二個「文件/」。我把它們拿出來,現在它工作正常。 – kayle 2013-04-22 21:23:15

+0

@kayle優秀。再次,對先前的評論感到抱歉。我會繼續討論如此多的問題,以致我的大腦瘋狂起來! – 2013-04-22 21:24:52