php move_uploaded_file() 不会替换现有图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9858861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
move_uploaded_file() won't replace existing image
提问by BlackMouse
I'm using the script below, so user can upload their profile picture. The first time an image is uploaded (when the image doesn't exist at the location) it works great. However, if the image is already exist at the path (if the user tries to change the profile picture) the new image won't replace the old one. I do get success for the query.
我正在使用下面的脚本,因此用户可以上传他们的个人资料图片。第一次上传图像时(当图像在该位置不存在时)效果很好。但是,如果图像已存在于路径中(如果用户尝试更改个人资料图片),则新图像不会替换旧图像。我确实获得了查询成功。
Any help would be very appreciated!
任何帮助将不胜感激!
Thanks
谢谢
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once('db.php');
$name = $_POST['name'];
$dir = '../uploadImages/';
$file = basename($_FILES['image']['name']);
$uploadfile = $dir . $file;
if(move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile))
{
$path = $name;
$path .= 'Image.png';
$query = mysql_query("UPDATE users SET imagePath='$path' WHERE username='$name'");
if ($query)
{
echo 'success';
}
else
{
echo 'error';
}
}
else
{
echo mysql_error();
}
?>
回答by Kishor Kundan
A better way would be un-linking the file if it exists
更好的方法是取消链接文件(如果存在)
if(file_exists('your-filename.ext')) {
chmod('your-filename.ext',0755); //Change the file permissions if allowed
unlink('your-filename.ext'); //remove the file
}
move_uploaded_files($_FILES['image']['tmp_name'], 'your-filename.ext');
回答by GolezTrol
If move_uploaded_file
fails, it returns false. In that case, no SQL is executed at all, so mysql_error
, which is echoed in the else
branch, indeed won't output an error.
如果move_uploaded_file
失败,则返回 false。在这种情况下,根本不执行任何 SQL,因此mysql_error
在else
分支中回显的确实不会输出错误。
if move_uploaded_file
fails, it issues a warning, that will become visible depending on your PHP settings. However, this problem doesn't have anything to do with MySQL.
如果move_uploaded_file
失败,它会发出警告,根据您的 PHP 设置,该警告将变得可见。但是,这个问题与 MySQL 没有任何关系。
If you try to explicitly delete the target file first, if it exists. Check with file_exists
and then with unlink
to delete the file. If unlink
fails, it's probably a permissions issue than won't allow you to delete or overwrite the file.
如果您尝试先显式删除目标文件(如果它存在)。检查 ,file_exists
然后用unlink
删除文件。如果unlink
失败,则可能是权限问题,不允许您删除或覆盖文件。
回答by Cristian Rodriguez
No, your logic is wrong.. take a look at the URL of your profile image, either here, in facebook or twitter..do you see they use a fixed predictable name ? They don't, and there is a very good reason for that, you need unique, unpredictable filenames.
不,你的逻辑是错误的......看看你的个人资料图片的 URL,无论是在这里,在 facebook 或 twitter..你看到他们使用固定的可预测名称吗?他们没有,而且有一个很好的理由,您需要唯一的、不可预测的文件名。
Try this:
尝试这个:
$file = hash('sha256', openssl_random_pseudo_bytes(8)) . 'yourallowedextension';
Then query the name of the old picture from your database, after that, upload the new pic, if that succeeds, update the user's profile picture in the database and unlink() the old file using the information previously obtained if any.
然后从您的数据库中查询旧图片的名称,然后上传新图片,如果成功,则更新数据库中用户的个人资料图片并使用先前获取的信息(如果有) unlink() 旧文件。
Ensure that you are not allowing to upload php files or any other nasty stuff, for that you can use php fileinfo extension.
确保您不允许上传 php 文件或任何其他讨厌的东西,为此您可以使用 php fileinfo 扩展名。
回答by Anup Sarkar
$file=$_FILES['image']['name'];
$path="your/location/".$file;
if(file_exists($path))
{
chmod($path,0755);
unlink($path);
}
Then move the file using move_uploaded_file()
.
然后使用move_uploaded_file()
.
回答by massoud.e
If you do this, the new image will be replaced:
如果这样做,新图像将被替换:
$sourcePath = $_FILES['image']['tmp_name'];
list($width,$height)=getimagesize($sourcePath);
$uploadedImage = imagecreatefromjpg($sourcePath);
$newImage=imagecreatetruecolor($newWidth,$newHeight);
imagecopyresampled($newImage,$uploadedImage,0,0,0,0,$newWidth,$newHeight,$width,$height);
imagejpeg($newImage, $destinationPath,100);