如何在保存到目录前重命名上传的文件?下面是我用来将文件上传到目录中的代码。效果很好。我的主要问题是:移动上载文件()是将上传的文件保存到目录中的文件,我也认为移动上载文件()是为它设置名字的那个。如何将文件名更改为随机数?我试图在以下方面这样做:$allowedExts = array("gif", "jpeg", "jpg", "png");$temp = explode(".", $_FILES["file"]["name"]);$extension = end($temp);if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 100000) && in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
$fileName = $temp[0] . "." . $temp[1];
$temp[0] = rand(0, 3000); //Set to random number
$fileName;
if (file_exists("../img/imageDirectory/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $_FILES["file"]["name"]);
echo "Stored in: " . "../img/imageDirectory/" . $_FILES["file"]["name"];
}
}} else {
echo "Invalid file";}我尝试更改变量,例如$_files[“file”][“name”]并将其替换为$文件名;变量,以便可以存储新名称。
3 回答
有只小跳蛙
TA贡献1824条经验 获得超8个赞
move_uploaded_file
.
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $_FILES["file"]["name"]);
$temp = explode(".", $_FILES["file"]["name"]);$newfilename = round(microtime(true)) . '.' . end($temp);move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);
拉风的咖菲猫
TA贡献1995条经验 获得超2个赞
$newfilename= date('dmYHis').str_replace(" ", "", basename($_FILES["file"]["name"]));move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);
慕斯王
TA贡献1864条经验 获得超2个赞
添加回答
举报
0/150
提交
取消