man5 Posted February 16, 2014 Share Posted February 16, 2014 I have created a form with which I can upload an image file path to the database and the image is uploaded to a relative folder in the directory. Now the problem is, every time I delete the image, it'll delete the file path in the database(Mysql), but it won't delete the image in the folder. I have tried unlink() many times and it just won't work. Below is the code you can look at and tell me what I am doing wrong. //upload.php if (isset($_FILES['image'])) { if (empty($_FILES['image']['name'])) { echo 'Please choose a file!'; } else { //Preparing the variables $name = $_FILES['image']['name']; $temp = $_FILES['image']['tmp_name']; $path = 'images/'; $location = $path.$name; try { $user = new User(); $data = DB::getInstance()->insert('guestbook', array( 'name' => $user->data()->username, 'path' => $location, 'message' => Input::get('message'), 'posted' => date('Y-m-d H:i:s') )); // Move the file. move_uploaded_file($temp, $location); Session::flash('home', 'You have created a new record!'); Redirect::to('upload.php'); } catch(Exception $e) { die($e->getMessage()); } } } //Here is the delete.php require_once 'core/init.php'; $user = new User(); // upload variables $name = $_FILES['image']['name']; $temp = $_FILES['image']['tmp_name']; $path = 'images/'; $location = $path.$name; try { if (file_exists($location)) { if(is_writable($location)) { if(unlink($location)) { echo "Successfully deleted " . $location; } else { echo "Problem deleting " . $location; } } } $delete = DB::getInstance()->delete('guestbook', array('path', '=', $user->data()->username)); Session::flash('home', 'your record has been deleted'); Redirect::to('test.php'); } catch(Exception $e) { die($e->getMessage()); } Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/ Share on other sites More sharing options...
Ch0cu3r Posted February 16, 2014 Share Posted February 16, 2014 The $_FILES['image'] array will only exist when the file has been uploaded to upload.php. That variable will not be available in delete.php. You will first need to get the users file from the database, and then pass their file path to unlink to delete that file Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469143 Share on other sites More sharing options...
man5 Posted February 16, 2014 Author Share Posted February 16, 2014 Could you possibly give me a demonstration? I am a little lost. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469179 Share on other sites More sharing options...
Ch0cu3r Posted February 16, 2014 Share Posted February 16, 2014 (edited) upload.php receives the file the user has uploaded from the $_FILES['images'] array. You are then storing the file path for the uploaded file in the 'path' column in your database, along with their name, message and post date $data = DB::getInstance()->insert('guestbook', array( 'name' => $user->data()->username, 'path' => $location, 'message' => Input::get('message'), 'posted' => date('Y-m-d H:i:s') )); This is the only time $_FILE['images'] will exist. When you go to delete.php, you are not uploading a file. Instead you are deleting a record from the database, along with deleting the users uploaded file. To know which file to delete you need fetch the record from the database where the username column matches $user->data()->username (see note below). So you can retrieve the file path for the file they have uploaded. You'd then pass that file path to unlink to remove the users uploaded file. NOTE: Finding the record by username will only work if the user can only add one entry to the guestbook. If they can post multiple entries then you'll need to pass in the id for the guestbook post instead. Edited February 16, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469181 Share on other sites More sharing options...
man5 Posted February 17, 2014 Author Share Posted February 17, 2014 I believe I did what you mentioned. I could be wrong. Here is actually the full page code. Upload.php require_once 'core/init.php'; $user = new User(); if($user->isLoggedIn()) { ?> <p>Hello <a href="profile.php?user=<?php echo escape($user->data()->username); ?>"><?php echo escape($user->data()->username); ?></a>!</p> <?php if(!Input::exists()) { ?> You can add your own picture (JPG, PNG, GIF) by selecting the image below.<br /><br /> <form method="post" action="" enctype="multipart/form-data"> <input type="text" name="message"> <input type="file" name="image"> <input type="submit" id="submit" value="submit"> </form> <a href="delete.php">delete image</a><br> <?php $image = DB::getInstance()->query("SELECT path FROM guestbook"); if(!$image->count()) { echo 'No image found'; } else { foreach($image->results() as $image) { echo '<img src="', $image->path, '">'; } } } else { if (isset($_FILES['image'])) { if (empty($_FILES['image']['name'])) { echo 'Please choose a file!'; } else { //Preparing the variables $name = $_FILES['image']['name']; $temp = $_FILES['image']['tmp_name']; $path = 'images/'; $location = $path.$name; try { $user = new User(); $data = DB::getInstance()->insert('guestbook', array( 'name' => $user->data()->username, 'path' => $location, 'message' => Input::get('message'), 'posted' => date('Y-m-d H:i:s') )); // Move the image. move_uploaded_file($temp, $location); Session::flash('home', 'You have created a new record!'); Redirect::to('test.php'); } catch(Exception $e) { die($e->getMessage()); } } } } } else { echo '<p>You need to <a href="login.php">log in</a> or <a href="register.php">Register</a> </p>'; } delete.php require_once 'core/init.php'; $user = new User(); if($user->isLoggedIn()) { // upload variables $name = $_FILES['image']['name']; $temp = $_FILES['image']['tmp_name']; $path = 'images/'; $location = $path.$name; try { unlink($location); $delete = DB::getInstance()->delete('guestbook', array('id', '=', 128)); Session::flash('home', 'your record has been deleted'); Redirect::to('test.php'); } catch(Exception $e) { die($e->getMessage()); } } Can you please edit the code to show me how to do it correctly? This problem has been bugging me for days now. Appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469186 Share on other sites More sharing options...
jcbones Posted February 17, 2014 Share Posted February 17, 2014 Now, you wouldn't learn anything if we did it all for you. If you would read carefully what Ch0cu3r wrote, you would know why and where the issue is. He even gave you a suggestion on fixing it.The $_FILES array ONLY exists if a file is sent via a form. Since you are deleting the info, then you need to get the file path from the database BEFORE you delete the database entry. Then pass that file path to the unlink() function. Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469187 Share on other sites More sharing options...
man5 Posted February 17, 2014 Author Share Posted February 17, 2014 I understand what both of you are saying. It's just that I am still new to php and for some reason with this problem, I am unable to grasp it fully. I feel the solution is rather simple. I'm just not seeing it yet. I have been at it for a long while now and the only way to move forward is to ask for help from a pro. Regarding getting the file path before deleting the database. Here are my changes. Still not working. $user = new User(); if($user->isLoggedIn()) { if (isset($_FILES['image'])) { $get = DB::getInstance()->get('guestbook', array('id', '=', 132)); // upload variables $name = $_FILES['image']['name']; $temp = $_FILES['image']['tmp_name']; $path = 'images/'; $location = $path.$name; unlink($location); try { $delete = DB::getInstance()->delete('guestbook', array('id', '=', 132)); Session::flash('home', 'your record has been deleted'); Redirect::to('test.php'); } catch(Exception $e) { die($e->getMessage()); } } } Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469192 Share on other sites More sharing options...
man5 Posted February 17, 2014 Author Share Posted February 17, 2014 Alright. I finally got it working with the code below. There is still more to add but i guess so far so good. $user = new User(); $del = DB::getInstance()->query("SELECT * FROM guestbook"); if(!$del->count()) { echo 'No image found'; } else { foreach($del->results() as $del) { unlink($del->path); } } try { $delete = DB::getInstance()->delete('guestbook', array('id', '=', 134)); Session::flash('home', 'your record has been deleted'); Redirect::to('test.php'); } catch(Exception $e) { die($e->getMessage()); } Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469194 Share on other sites More sharing options...
Ch0cu3r Posted February 17, 2014 Share Posted February 17, 2014 (edited) This $del = DB::getInstance()->query("SELECT * FROM guestbook"); if(!$del->count()) { echo 'No image found'; } else { foreach($del->results() as $del) { unlink($del->path); } } Will delete all the files that are recorded in the database. You'll need to run the following query to delete a file associated with a specific post. $get = DB::getInstance()->get('guestbook', array('id', '=', 132)); // get the guestbook entry that matches id 132 unlink($get->path); // delete the file associated with it Edited February 17, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469240 Share on other sites More sharing options...
man5 Posted February 17, 2014 Author Share Posted February 17, 2014 Got it. Thanks. One other question. On the upload page, how do I set up $id for each record so that it automacially correspond to the deletion of each record by the user individually; as oppose to me entering it manually? Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469244 Share on other sites More sharing options...
Ch0cu3r Posted February 17, 2014 Share Posted February 17, 2014 Pass the post id in the url, eg echo '<a href="delete.php?id='. $post_id . '">Delete Post</a>'; Then use $_GET['id'] in delete.php to get the posts id. Example if(isset($_GET['id'])) { $id = intval($_GET['id']); // delete file $get = DB::getInstance()->get('guestbook', array('id', '=', $id)); unlink($get->path); // delete post $delete = DB::getInstance()->delete('guestbook', array('id', '=', $id)); } However this will allow anyone to delete a post. If you only want to allow the person that created the post then you'll need to check for that. Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469247 Share on other sites More sharing options...
man5 Posted February 17, 2014 Author Share Posted February 17, 2014 Below is the updated code with your changes. Though I must ask, $post_id is not defined in delete.php . Only $id is. delete.php If(isset($_GET['id'])) { $user = new User(); $id = intval($_GET['id']); //delete file $get = DB::getInstance()->get('guestbook', array('id', '=', $id)); if(!$get->count()) { echo 'No image found'; } else { foreach($get->results() as $get) { unlink($get->path); } } try { //delete post $delete = DB::getInstance()->delete('guestbook', array('id', '=', $id)); Session::flash('home', 'your record has been deleted'); Redirect::to('test.php'); } catch(Exception $e) { die($e->getMessage()); } } upload.php $user = new User(); if($user->isLoggedIn()) { ?> <p>Hello <a href="profile.php?user=<?php echo escape($user->data()->username); ?>"><?php echo escape($user->data()->username); ?></a>!</p> <?php if(!Input::exists()) { ?> You can add your own picture (JPG, PNG) by selecting the image below.<br /><br /> <form method="post" action="" enctype="multipart/form-data"> <input type="text" name="message"> <input type="file" name="image"> <input type="submit" id="submit" value="submit"> </form> <?php $image = DB::getInstance()->query("SELECT * FROM guestbook"); if(!$image->count()) { echo 'No image found'; } else { foreach($image->results() as $image) { ?> <div id="image-1"> <a href="delete.php?id='. $post_id .'">Delete Post</a><br> <div class="images"> <?php echo '<img src="', $image->path, '">'; ?> </div> </div> <?php } } } else { if (isset($_FILES['image'])) { if (empty($_FILES['image']['name'])) { echo 'Please choose a file!'; } else { //Preparing the variables $name = $_FILES['image']['name']; $temp = $_FILES['image']['tmp_name']; $path = 'images/'; $location = $path.$name; try { $user = new User(); $data = DB::getInstance()->insert('guestbook', array( 'name' => $user->data()->username, 'path' => $location, 'message' => Input::get('message'), 'posted' => date('Y-m-d H:i:s') )); // Move the original file aswell. move_uploaded_file($temp, $location); Session::flash('home', 'You have created a new deal!'); Redirect::to('test.php'); } catch(Exception $e) { die($e->getMessage()); } } } } } else { echo '<p>You need to <a href="login.php">log in</a> or <a href="register.php">Register</a> </p>'; } Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469262 Share on other sites More sharing options...
Ch0cu3r Posted February 17, 2014 Share Posted February 17, 2014 Well obviously you need to define the $post_id variable with the posts id. I do not know what variable that is so I gave you an example. You should know what that variable is if you understand what the code here does $image = DB::getInstance()->query("SELECT * FROM guestbook"); ... foreach($image->results() as $image) { ... } Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469265 Share on other sites More sharing options...
man5 Posted February 17, 2014 Author Share Posted February 17, 2014 Sorry you lost me there for a second. Do you mean match the $post_id with the posts 'id' in the database? 'id' => $post_id, Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469278 Share on other sites More sharing options...
Ch0cu3r Posted February 17, 2014 Share Posted February 17, 2014 The foreach loop is looping over the results of the select query. $image contains the data for the current row (id, path, message, date,... etc) . $image->id will contain the post id you need to use in your delete link foreach($image->results() as $image) { ?> <div id="image-1"> <a href="delete.php?id=<?php echo $image->id; ?>">Delete Post</a><br> <div class="images"> <?php echo '<img src="', $image->path, '">'; ?> </div> </div> <?php } Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469279 Share on other sites More sharing options...
man5 Posted February 17, 2014 Author Share Posted February 17, 2014 Wow. It works. Perfect! Thanks so much CHocu3r. You have been a great help! Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469281 Share on other sites More sharing options...
man5 Posted February 17, 2014 Author Share Posted February 17, 2014 One last thing I forgot to mention. The original image upload script i have resizes the image, creates two instances(original and thumb_) in the folder, and renames the images with random random. It also stores the thumb_filepath to the path column in the database. The only problem with this is, every time I delete the image, it will only delete the thumb_ image. I don't suppose there is a way to delete the original image as well...from the folder? In summary, Image upload script does the following. 1. Resizes image 2. Creates two images. Original and thumb_ 3. Creates random name for each image. eg. "66df8da615e950a049fe2931d6cbeedc.jpg" Do you think this is a correct way to do image uploads? Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469285 Share on other sites More sharing options...
jazzman1 Posted February 17, 2014 Share Posted February 17, 2014 I prefer to name the image with the same value of id column. Assuming the column is - id integer not null auto_increment. Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469287 Share on other sites More sharing options...
man5 Posted February 17, 2014 Author Share Posted February 17, 2014 So I have come a conculsion to include 2 path columns in the database. "path" and "thumb_path". Unlink both of them and it does the trick. Quote Link to comment https://forums.phpfreaks.com/topic/286234-help-with-uploaded-images-not-deleting-from-folder/#findComment-1469295 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.