Search the Community
Showing results for tags 'delete file'.
-
I am using WAMP to run PHP code and MYSQL database So, I am working on a project that involves setting up a user with a profile picture. I was able to add a profile picture easily, but then as I realized I should implement a way to delete the old profile picture BEFORE changing to a new uploaded image by the user otherwise my directory will fill up with old pictures which is not what I want for a future application online with limited storage. Anyways, before I post my code, I must state that all of the folders involved have read and write permissions (no .htaccess involved). Here is the (delete_old_prof_img) function code : function delete_old_prof_img($user_id) { $old_path = mysql_query("SELECT `prof_img` FROM `users` WHERE `user_id` = " . (int)$user_id); // Find the old profile image if($old_path == "") // If it has nothing in the profile iamge column just return true { return true; } else if(unlink($old_path) == true && $old_path !== "") // If it is succesfully removed and the old path doesn't equal an empty string { mysql_query("UPDATE `users` SET `prof_img` = '' WHERE `old_prof_img` = '" . $old_path . "'"); // Reset the profile image column return true; } else // If image couldn't be removed just return false { return false; } } When I tried copying the $old_path into the mysql database into a column it returns "Resource id#14." The weirdest thing that I noticed was that the unlink function works fine if I put in the relative location such as unlink('images/profile/myimg.jpg') . Although, when I run the "delete_old_prof_img" function, the image in my directory is not removed, and it returns false. The database has the correct relative link to the location of the file in the 'prof_img'. Here is the page code it is used in. Please notice this is an nested conditional within others dealing with the html form. $allowed = array('jpg', 'jpeg', 'gif', 'png'); // Allowed extensions to be uploaded $file_name = $_FILES['prof_img']['name']; // File name of the image uploaded $file_extn = strtolower(end(explode('.', $file_name))); // File extension of the image uploaded $file_temp = $_FILES['prof_img']['tmp_name']; // The temporary file name if (in_array($file_extn, $allowed) === true) // If the file uploaded is in the list of those allowed to be uploaded, continue. { if(delete_old_prof_img($session_user_id) == true) // If the old image is deleted, continue. { change_prof_img($session_user_id, $file_temp, $file_extn); // Change the profile image to the newly uploaded one. header('Location: settings.php?upload_success'); // Return to a success page. exit(); // Exit } else if(delete_old_prof_img($session_user_id) == false) // If the old image cannot be deleted, continue. { header('Location: settings.php?upload_failure'); // Return to a failure page. exit(); // Exit } else // First time uploading { change_prof_img($session_user_id, $file_temp, $file_extn); // Change profile image header('Location: settings.php?upload_success'); // Return to success page. exit(); // Exit } } else { $errors[] = 'Incorrect file type. Allowed: ' . implode(', ', $allowed); // Put errors in the error array if not allowed file types, to be returned later. } I have turned on error reporting, but nothing has come up. Any redesign is appreciated, if this can be more simplified. Thank you for your time.