2015-02-10 121 views
0

我在我的代碼中有疑問。我正在創建一個表格來註冊學生圖片。上傳到文件夾時重命名圖像時出錯

當兩個學生輸入相同的圖像名稱(image.jpg)時,我想重命名它並保存在同一個文件夾中。

但問題是我能夠重命名它,但我不能將圖像移動到保存文件夾。

上傳 - >相同圖像 - >重命名 - >無法保存在文件夾中。

我的代碼如下:

我試着使用一些變化。但無法實現它。

if(isset($_FILES["file"]["type"])) 
{ 
$validextensions = array("jpeg", "jpg", "png"); 
$temporary = explode(".", $_FILES["file"]["name"]); 
$file_extension = end($temporary); 

if ((($_FILES["file"]["type"] == "image/png")||($_FILES["file"]["type"] == "image/jpg")||($_FILES["file"]["type"] == "image/jpeg")) && in_array($file_extension, $validextensions)) 
{ 
if ($_FILES["file"]["error"] > 0) 
{ 
echo "Return Code: " . $_FILES["file"]["error"] . "<br/><br/>"; 
} 
else 
{ 

$sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable 
$filename = $_FILES['file']['name']; // Target path where file is to be stored 
move_uploaded_file($sourcePath, "$upload/$filename"); 

$random_digit = rand(0000,9999); 
$new_file_name = $filename.$random_digit; 


$select = mysql_query("insert into application_form (mname, mgender, mtxtday, mtxtMonth, mtxtYear, mage, mnationality, mreligion, mcommunity, maddress, mphone, memailid, mdistan_resid, mmother_name, mmother_occu, mmother_quali, mmother_address, mmother_phone, mmother_email, mmother_income, mfather_name, mfather_occu, mfather_quali, mfather_address, mfather_phone, mfather_email, mfather_income, mguard_name, mguard_occu, mguard_quali, mguard_address, mguard_phone, mguard_email, mguard_income, mclass_admis, mlast_class, msecond_lang, mmother_tongue, mmode_transport, mvaccinat, mno_of_brosis, mplace, mtoday_date, magree, mpass_photo) values ('$sname','$sgender','$stxtday','$stxtMonth','$stxtYear','$sage','$snationality','$sreligion','$scommunity','$saddress','$sphone','$semailid','$sdistan_resid','$smother_name','$smother_occu','$smother_quali','$smother_address','$smother_phone','$smother_email','$smother_income','$sfather_name','$sfather_occu','$sfather_quali','$sfather_address','$sfather_phone','$sfather_email','$sfather_income','$sguard_name','$sguard_occu','$sguard_quali','$sguard_address','$sguard_phone','$sguard_email','$sguard_income','$sclass_admis','$slast_class','$ssecond_lang','$smother_tongue','$smode_transport','$svaccinat','$sno_of_brosis','$splace','$stoday_date','$agree','$new_file_name')"); 

$last_insert_id = mysql_insert_id(); 

//echo "Last insert id in employee table: ".$last_insert_id; 

} 
} 
else 
{ 
echo "<span id='invalid'>***Invalid file Type***<span>"; 
} 
} 

好心幫我在這個問題..我是否需要更正這個代碼什麼..

或任何人可以幫助我任何其它的代碼或想法。

在此先感謝。

回答

0

要替換的文件,移動文件在磁盤上使用新名稱 使用此:

$sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable 
$filename = $_FILES['file']['name']; // Target path where file is to be stored 
$random_digit = rand(0000,9999); 
$new_file_name = $filename.$random_digit; 
move_uploaded_file($sourcePath, "$upload/$new_file_name");