gpt4 book ai didi

php - 如何在通过php存储在文件夹中时更改图像名称

转载 作者:搜寻专家 更新时间:2023-10-30 22:16:47 26 4
gpt4 key购买 nike

我在一个网站上工作,每个用户都可以上传多张图片..我可以上传图片并存储在一个文件夹中,但是当两个不同的用户有相同的图片名称时我遇到了问题,就像我一样将图像存储在单个文件夹中。在我存储信息的数据库中,序列号是唯一键。我想通过添加序列号来重命名图像。使用图像名称使其独一无二...请帮助我..做到这一点..

my form is as:-

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" /> </br><br/>
<input id="submit" type="submit" value="submit" />

upload_file.php 为:-

require_once('dbconnect.inc.php');
$db_name="uploaddata";
$tbl_name="data";
$db_selected=mysql_select_db("$db_name")or die("cannot select DB");
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] > 0))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
$Image_name=$_FILES["file"]["name"];
$Image_type=$_FILES["file"]["type"];
//upload is the folder of storing images
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
$_SESSION['response'] = " already exists please give some different name ";
header("Location: testimage.php");
}
else
{

move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
$login_data="INSERT INTO $tbl_name(Image_name,Image_type) VALUES('$Image_name','$Image_type')";

$login_result=mysql_query($login_data) or die(mysql_error());
header("Location: abc.php");
}
}
}

最佳答案

您可以使用 move_uploaded_file 函数更改目标文件的名称,例如

move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $serial_number . '_' . $_FILES["file"]["name"] . );

您也可以使用这个 function

bool rename ( string $oldname , string $newname [, resource $context ] );

关于php - 如何在通过php存储在文件夹中时更改图像名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10929726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com