Move_uploaded_file () will not replace an existing image

I use the script below, so the user can upload their profile picture. The first time the image is loaded (when the image does not exist in place), it works great. However, if the image already exists in the path (if the user tries to change the profile image), the new image will not be replaced with the old one. I get success for the request.

Any help would be greatly 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(); } ?> 
+4
source share
5 answers

The best way would be to not link 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'); 
+20
source

If move_uploaded_file does not work, it returns false. In this case, SQL is not executed at all, so mysql_error , which is displayed in the else branch, really does not throw an error.

If move_uploaded_file fails, it issues a warning that will become visible depending on your PHP settings. However, this problem has nothing to do with MySQL.

If you first try to delete the target file, if it exists. Check file_exists and then unlink to delete the file. If unlink fails, this is probably a permission issue, which will prevent you from deleting or overwriting the file.

+4
source

No, your logic is wrong. Look at the URL of your profile picture, either here, either on facebook or twitter ... do you see that they use a fixed predictable name? They do not, and there is a very good reason for this; you need unique, unpredictable file names.

Try the following:

 $file = hash('sha256', openssl_random_pseudo_bytes(8)) . 'yourallowedextension'; 

Then request the name of the old image from your database, then upload a new image, if it succeeds, update the user profile image in the database and disconnect () the old file using the information obtained earlier, if any.

Make sure that you do not allow php files or any other unpleasant things to be downloaded, for which you can use the php fileinfo extension.

+2
source
 $file=$_FILES['image']['name']; $path="your/location/".$file; if(file_exists($path) { chmod($path,0755); unlink($path); } 

then move the file.

+1
source

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); 
0
source

Source: https://habr.com/ru/post/1403300/


All Articles