【问题标题】:error in renaming images while uploading to folder上传到文件夹时重命名图像时出错
【发布时间】:2015-02-10 10:14:13
【问题描述】:

我对我的代码有疑问。我正在创建一个表格来注册学生图片。

当两个学生输入相同的图像名称(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>";
}
}

请在这个问题上帮助我..我是否需要更正这段代码中的任何内容..

或者任何人都可以帮助我提供任何其他代码或想法。

提前致谢。

【问题讨论】:

    标签: javascript forms file-upload


    【解决方案1】:

    您正在替换文件,用新名称移动磁盘上的文件 使用这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 2016-12-02
      相关资源
      最近更新 更多