First of all, I'll request to not mark my question as duplicate. Because I have googled my problem a lot and didn't find solution of my problem. There is a lot of problem of permission denied on unlink
but my problem for this is totally different. So, first read my problem carefully below.
I am creating a user based website. On profile edit, i am updating profile picture. Currently, there is no image on users database, but i am checking in backend that if profile pic exists in user db, then unlink (delete) that first from folder and then update with new one. But i am getting permission denied error on unlink if there is no image. But if there is image, i didn't get any permission denied error. So all i need to call that unlink function only on that condition if user image exists in db. But I've tried to do this a lot. I am failed again and again.
Here is my function code:
public function updateProfilePic($file, $userid) {
$this->selectImgOnUpdating($userid);
$filename = $file['user_img']['name'];
$filetmp = $file['user_img']['tmp_name'];
$valid_ext = array('png', 'jpeg', 'jpg');
$location = "user/profilepic/" . $filename;
$file_extension = pathinfo($location, PATHINFO_EXTENSION);
$file_extensionstr = strtolower($file_extension);
if (in_array($file_extensionstr, $valid_ext)) {
$this->compressImage($filetmp, $location, 60);
$updateuserpic = $this->updateProfilePicture($filename, $userid);
return $updateuserpic;
} else {
$msg = '<div class="alert alert-danger" role="alert">Invalid file type. You can upload only:-' . implode(', ', $valid_ext) . '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button></div>';
return $msg;
}
}
public function selectImgOnUpdating($userid){
$query = "SELECT user_img from users WHERE user_id = '$userid'";
$result = $this->db->select($query);
if ($result) {
$deleteImg = $result->fetch_assoc();
$deleteLink = $deleteImg['user_img'];
$path = "user/profilepic/$deleteLink";
unlink($path);
}
}
public function updateProfilePicture($filename, $userid){
$update = "UPDATE users SET user_img = '$filename' WHERE user_id = '$userid'";
$result = $this->db->update($update);
if($result){
$msg = '<div class="alert alert-success" role="alert">Profile picture uploaded successfully <a href="profile.php">Go back</a><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button></div>';
return $msg;
} else {
$msg = '<div class="alert alert-danger" role="alert">Error while uploading profile picture. Pleas try again!<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button></div>';
return $msg;
}
}
Now, i've tried to put check condition first on fetching.
public function selectImgOnUpdating($userid){
$query = "SELECT user_img from users WHERE user_id = '$userid'";
$result = $this->db->select($query);
if ($result) {
$deleteImg = $result->fetch_assoc();
$deleteLink = $deleteImg['user_img'];
$count = $result->num_rows;
if($count > 0){
$path = "user/profilepic/$deleteLink";
unlink($path);
}
}
}
It's also not working, i am still getting that permission denied error.
On edit profile, there is one button named "Update profile pic", and new user can upload/update from that.
Currently there is no image in users database row. (So, i am still updating it, because i am doing it with only one function/page. ) On update function, i am first fetching user image, and want to check that if user image exists there then delete/unlink that from folder and then run update function below.