Jump to content

Help with uploaded images not deleting from folder


man5

Recommended Posts

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());
} 
Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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());
		}

	}
	
}
Link to comment
Share on other sites

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());
}

Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
} 
Link to comment
Share on other sites

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) {
    ...
}

 

Link to comment
Share on other sites

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
		}
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.