Javascript 上载到文件夹时重命名图像时出错

Javascript 上载到文件夹时重命名图像时出错,javascript,forms,file-upload,Javascript,Forms,File Upload,我的密码有问题。我正在创建一个表单来注册学生图片 当两个学生输入相同的图像名称时,我想将其重命名并保存在同一文件夹中 但问题是我可以重命名它,但我不能将图像移动到文件夹中保存 上传->相同图像->重命名->无法保存在文件夹中 我的代码如下: 我试着做了一些改变。但却无法实现 if(isset($_FILES["file"]["type"])) { $validextensions = array("jpeg", "jpg", "png"); $temporary = explode(".", $

我的密码有问题。我正在创建一个表单来注册学生图片

当两个学生输入相同的图像名称时,我想将其重命名并保存在同一文件夹中

但问题是我可以重命名它,但我不能将图像移动到文件夹中保存

上传->相同图像->重命名->无法保存在文件夹中

我的代码如下:

我试着做了一些改变。但却无法实现

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>";
}
}
if(isset($\u文件[“文件”][“类型”]))
{
$validextensions=数组(“jpeg”、“jpg”、“png”);
$temporary=explode(“.”,$_文件[“文件”][“名称”]);
$file_extension=end($temporary);
如果($_文件[“文件”][“类型”]=“图像/png”)| |($_文件[“文件”][“类型”]=“图像/jpg”)| |($_文件[“文件”][“类型”]=“图像/jpeg”)&&in_数组($file_扩展名,$validextensions))
{
如果($\u文件[“文件”][“错误”]>0)
{
回显“返回代码:”.$\u文件[“文件”][“错误”]。“

”; } 其他的 { $sourcePath=$\u FILES['file']['tmp\u name'];//将文件的源路径存储在变量中 $filename=$_FILES['file']['name'];//存储文件的目标路径 移动上传的文件($sourcePath,“$upload/$filename”); $random_digit=兰特(00009999); $new_file_name=$filename.$random_digit; $select=mysql\u查询(“插入应用程序\u表单(名称、mgender、mtxtday、MTXTMOUNT、mtxtYear、mage、国家、宗教、社区、MADDRENT、mphone、memailid、mdistan、mmother、mmother、mmother、mtxtYear、mmother、mmother电话、mmother电子邮件、mmother收入、mfather姓名、mfather职业、mfather质量、mfather地址、mfather电话、mfather收入、mgua)rd(姓名),mguard(职业),mguard(质量),mguard(地址),mguard(电话),mguard(电子邮件),mguard(收入),mclass(行政),mlast LAST(班级),MSSecond lang,mmother(语言),mmode 40“2007年,,“$斯奈米”和“$斯奈米”以及“$斯奈米”和“$斯奈米”以及“$斯奈米“,“$斯奈米“,“$斯奈米”和“$斯奈米”以及““$斯奈米”和“$斯奈米”和“$斯奈德”,““$斯奈德德”,“未来未来未来未来未来,,,,“$斯奈德”,“未来未来未来,,,,,,,“$斯奈米”,“未来,,,,,,,,,,,,,,,“$斯奈米”,“未来,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,地址','$“足球运动员的手机”和“足球运动员的手机”和,“足球运动员的手机”和,“足球运动员的手机”和,“足球运动员的手机”和,“足球运动员的手机”和,“足球运动员的手机”和,“足球运动员的手机”和,“足球运动员的手机”和,“足球运动员的手机”和,“足球运动员的电子邮件”和,“足球运动员的收入”,和“收入收入收入收入收入收入收入”,以及“足球运动员的收入收入收入”,以及“$斯瓜德们们的名字名字,,,,“$斯瓜德将将将将将发生发生的人和他们他们的名字,,,,,,“$斯瓜德将将将将发生发生发生发生发生发生的人和他们他们他们他们的事情,他们他们他们他们的事情,他们他们他们的发生发生发生的事情,他们他们他们的事情,他们他们他们他们他们的事情,他们的名字,,,“他们将将将将将发生发生发生发生的事情,他们他们他们他们他们的人身身身身身身身身身身身身身身身身新文件名“)”; $last_insert_id=mysql_insert_id(); //echo“员工表中的最后一个插入id:”.$Last\u insert\u id; } } 其他的 { 回显“***无效文件类型***”; } }
请在这个问题上帮助我..我是否需要更正此代码中的任何内容

或者有人能帮我写一些其他的代码或想法吗


提前感谢。

如果要替换文件,请使用新名称移动磁盘上的文件 使用以下命令:

$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");