php script to delete file from directory & database value

Hi All,

You guys have helped me to develop an upload script previously but now I wanted to allow users to delete files as well.

I can create a delete script easily enough for the database value but I struggle when it comes to deleting the file from one of server directories.

My delete script so far is…

[php]<?php

// check ‘id’ value is set
if(isset($_GET[‘d’])) {
}

// check all our variables are set
if (isset($_POST[“del”]))
{

// call database…
$sql = “DELETE FROM tbl_documents WHERE client_id=’{$_SESSION[‘client_id_of_user’]}’ AND file_name =” .$_GET[‘d’] ;

	if ($db->query($sql) === TRUE) { // will return true if successful else it will return false
			
			echo "<script>location.href='index.php'</script>";
				}
	
			else {
			 	// if the database was not updated then inform user
				echo "<div class='alert alert-danger alert-dismissible' role='alert'>
		<button type='button' class='close' data-dismiss='alert'><span aria-hidden='true'>&times;</span><span class='sr-only'>Close</span></button>
		<strong>Error!</strong> File not deleted!!!</div>";
		
		}

}
?>
[/php]

My html is along these lines…
[php]

Are you sure you wish to delete this file?

Yes No

[/php]

My files are located in a folder “www.website.com/directory/pages/documents/uploads/

Help us to help you Dan. You have done the proper first part when asking for help on a coding forum, You showed us what you tried or have.

Steps to get the right help:
Answer to forum, What you have tried, what was the result, and what was the expected result.

So lets start with the basic task at hand, deleting a file. Do you know, or have you searched google for the command that deletes a file? A simple google search easily gives the answer. Pretty much every question a beginner will have has been asked and answered and a simple google search will usually get you the answer or head you in the right direction.

As I said in another post to you, we dont mind helping when you get stuck or dont understand something, but you need to put in a little effort first.

Show us that you looked up the command to delete a file on a server and we will go from there.

Hi kevin, no worries… I will reply with my findings/update

Looking forward to teaching you. :smiley:

Dan, look up the php UNLINK() function. But, you should also make sure that you add a pop-up or a
second notice that shows up to make sure that the user really wants to delete the file. There are very nice
ways to hide a

and then show it when the user presses the delete button. Then, inside the newly
shown
you have the real delete button for the form. This just makes the user think about the delete
being real or not…

Just more info to think about…

Hi Guys, I researched a bit and looked on the php.net website and straight away the unlink function popped up.

I’ve written this into my script and tested it… only got it working first time! :smiley: well chuffed. At the moment it is lacking the error to display if the file was not deleted (unlink function).

My current code is:-
[php] <?php

   include('../../includes/mysqli_connect.php');
   
if (mysqli_connect_errno($db)) {
   trigger_error('Database connection failed: '  . mysqli_connect_error(), E_USER_ERROR);

}

// check ‘id’ value is set
if(isset($_GET[‘d’])) {
}

// check all our variables are set
if (isset($_POST[“del”]))
{

$UploadDirectory = “uploads/”;
$target = $UploadDirectory . $_POST[‘file_name’];

$file_name = $_POST[‘file_name’];

// call database…
$sql = “DELETE FROM tbl_documents WHERE client_id=’{$_SESSION[‘client_id_of_user’]}’ AND id =” .$_GET[‘d’] ;

	if ($db->query($sql) === TRUE) { // will return true if successful else it will return false
			unlink($target);		
			echo "<script>location.href='index.php'</script>";
				}
	
			else {
			 	// if the database was not updated then inform user
				echo "<div class='alert alert-danger alert-dismissible' role='alert'>
		<button type='button' class='close' data-dismiss='alert'><span aria-hidden='true'>&times;</span><span class='sr-only'>Close</span></button>
		<strong>Error!</strong> File not deleted!!!</div>";
		
		}

}

// get stored form data.
// display update form, pre-populated with the stored data.
$query = “SELECT * FROM tbl_documents WHERE client_id=’{$_SESSION[‘client_id_of_user’]}’ AND id=”.$_GET[‘d’];
$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

?>
[/php]

ErnieAlex - Thanks, I’ve created a yes/no option as below;-
[php]

				  <div class="form-group">
				    <label class="col-sm-2">Client Id</label>
				    <div class="col-sm-4">
				      	<input name="client_id" type="text" class="form-control" value="<?php echo $row['client_id'];?>" readonly>
				    </div>
				  </div>

				  <div class="form-group">
				    <label class="col-sm-2">File Name</label>
				    <div class="col-sm-4">
				      	<input name="file_name" type="text"   class="form-control" value="<?php echo $row['file_name'];?>" readonly>
				    </div>
				  </div>

Are you sure you want to delete this file?

Yes No
[/php]

Great that you did it yourself… You can mark this one solved or we can do it if you wish…
Or did you have more questions on this subject?

Always nice to solve your own programming puzzle! Good for you!

So tell us, wasn’t there a certain satisfaction that you looked it up and got it to work on your own? That’s the buzz I get from programming, coming up with solutions to a problem on my own.

;D Yep, I agree ! ;D

Sponsor our Newsletter | Privacy Policy | Terms of Service