2013-12-08 24 views
-1

我試圖創建一個腳本,添加一個隨機號碼。在上傳的文件,然後將其上傳到服務器我在做什麼錯誤的上傳腳本

//This line assigns a random number to a variable. You could also use a timestamp here if you prefer. 
$ran = rand() ; 

//This takes the random number (or timestamp) you generated and adds a _ on the end, so it is ready of the file extension to be appended. 
$ran2 = $ran."_"; 

//This gets all the other information from the form 
$name=$_POST['name']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$pic=$ran2.$_FILES['uploaded']['name']; 

//escape User Input to help prevent SQL Injection 
$name= mysql_real_escape_string($name); 
$email= mysql_real_escape_string($email); 
$phone= mysql_real_escape_string($phone); 
$pic= mysql_real_escape_string($pic); 

// Connects to your Database 
mysql_connect("example.com", "user", "password") or die(mysql_error()) ; 
mysql_select_db("database") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO `table` VALUES ('$name', '$email', '$phone', '$pic')") ; 



//This assigns the subdirectory you want to save into... make sure it exists! 
$target = "images/"; 


//This combines the directory, the random file name, and the extension 
$target = $target . $pic; 


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

//Tells you if its all ok 
echo "file uploaded in ".$target; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 

儘管我認爲這個問題是在move_uploaded_file或$ PIC = $ RAN2。$ _ FILES [ '上傳'] [ '名']。請幫我糾正它。

+0

請發表您的表格! –

回答

0

使用複製功能代替move_uploaded_file來複制&重命名上傳的文件。

copy($_FILES['uploaded']['tmp_name'], $target); 
+0

不,告訴文件名爲空。 – user3079404

+0

請發佈您的html表格代碼。是輸入名稱'上傳'? –

+0

非常感謝,形式上存在問題,但如果沒有您的複印功能幫助,就不會發生這種情況。謝謝 – user3079404