上传时重命名文件
我有一个非常基本的文件上传脚本:
$prefix = "sitename";
$targetPath = "/uploads/sitename/"
$prefix = $prefix . "_";
$filename = $prefix . md5(basename($_FILES['image']['name'])) . md5(time()) . rand(11111,99999);
if(move_uploaded_file($_FILES['image']['tmp_name '], $targetPath)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
但我需要将图像重命名为 $filename
,然后才能移动到 $targetPath
中设置的位置。我该怎么办?
I have this very basic file upload script:
$prefix = "sitename";
$targetPath = "/uploads/sitename/"
$prefix = $prefix . "_";
$filename = $prefix . md5(basename($_FILES['image']['name'])) . md5(time()) . rand(11111,99999);
if(move_uploaded_file($_FILES['image']['tmp_name '], $targetPath)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
But I need to rename the image to $filename
before it moves to the location set in $targetPath
. What do I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在
move_uploaded_file
的第二个参数中指定新文件名。示例:
You can specify the new filename in the second parameter of
move_uploaded_file
.Example: