我对我的代码有疑问。我正在创建一个表单来注册学生图像。
当两个学生输入相同的图像名称时(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 :(得分: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");