2015-02-10 111 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");