flapjacksmike Posted November 1, 2012 Share Posted November 1, 2012 (edited) 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. Edited November 1, 2012 by flapjacksmike Quote Link to comment https://forums.phpfreaks.com/topic/270166-unlinking-file-problem/ Share on other sites More sharing options...
akphidelt2007 Posted November 1, 2012 Share Posted November 1, 2012 Do a print_r on $old_path after you run the query and you'll see your problem Quote Link to comment https://forums.phpfreaks.com/topic/270166-unlinking-file-problem/#findComment-1389371 Share on other sites More sharing options...
akphidelt2007 Posted November 1, 2012 Share Posted November 1, 2012 Oh I didn't read all the way through, apparently you did try using $old_path and you came up with the resource id. Try this. $old_path = mysql_query("SELECT `prof_img` FROM `users` WHERE `user_id` = " . (int)$user_id); $old_path = mysql_num_rows($old_path)==0 ? false : mysql_result($old_path,0); Quote Link to comment https://forums.phpfreaks.com/topic/270166-unlinking-file-problem/#findComment-1389376 Share on other sites More sharing options...
flapjacksmike Posted November 1, 2012 Author Share Posted November 1, 2012 (edited) Thanks for your help. Um, I tried substituting the above code in for the function (delete_old_prof_img), and it still returns false. Also now $old_path returns Resource id #15 into the mysql database. Edited November 1, 2012 by flapjacksmike Quote Link to comment https://forums.phpfreaks.com/topic/270166-unlinking-file-problem/#findComment-1389378 Share on other sites More sharing options...
akphidelt2007 Posted November 1, 2012 Share Posted November 1, 2012 Well most likely in another one of your function you are updating the database. Because if you put that code in to your delete old prof function you would not end up with the resource id. What I'm guessing is change_prof_img is doing the update/insert and coming up with the resource id. Quote Link to comment https://forums.phpfreaks.com/topic/270166-unlinking-file-problem/#findComment-1389388 Share on other sites More sharing options...
flapjacksmike Posted November 1, 2012 Author Share Posted November 1, 2012 (edited) Here is change_prof_img: function change_prof_img($user_id, $file_temp, $file_extn) { $file_path = 'images/profile/' . substr(md5(time()),0,10) . '.' . $file_extn; // Assigning the file path for the newly uploaded image move_uploaded_file($file_temp, $file_path); // Move the temporary file of the uploaded file to the new file path mysql_query("UPDATE `users` SET `prof_img` = '" . $file_path . "' WHERE `user_id` = " . (int)$user_id); // Put this new file path into the 'prof_img' column. } Edited November 1, 2012 by flapjacksmike Quote Link to comment https://forums.phpfreaks.com/topic/270166-unlinking-file-problem/#findComment-1389389 Share on other sites More sharing options...
akphidelt2007 Posted November 1, 2012 Share Posted November 1, 2012 Well than that can't be it. Show the updated delete_old_prof_img function. Quote Link to comment https://forums.phpfreaks.com/topic/270166-unlinking-file-problem/#findComment-1389390 Share on other sites More sharing options...
flapjacksmike Posted November 1, 2012 Author Share Posted November 1, 2012 (edited) Never mind. I had figured it out, when I checked errors my $session_user_id was an undefined variable so I had to declare it at the top of the page as a global variable, and changed delete_old_prof_img as follows: $old_path = mysql_query("SELECT `prof_img` FROM `users` WHERE `user_id` = " . (int)$user_id); $old_path = (mysql_num_rows($old_path)==0) ? false : mysql_result($old_path,0); if($old_path !== false) { unlink($old_path); return true; } else { return false; } Then it appears to have worked, I will run some further testing, and the previous image is now being deleted from the directory. Thank you for your help. Edited November 1, 2012 by flapjacksmike Quote Link to comment https://forums.phpfreaks.com/topic/270166-unlinking-file-problem/#findComment-1389394 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.