?
Solved

delete an image from the database folder

Posted on 2010-09-01
4
Medium Priority
?
398 Views
Last Modified: 2012-05-10
Hi, I need help once again. This time with deleting images from the 'uploads' database folder.

I have no problems deleting the item from the database but cant find a way of deleting the related image from the folder (../../uploads).

I have looked through some of the solution posted on this site, but cant find a solution as each question in unique.

I have add to my code (attached) but get an error?

$path = '../../uploads/'; //path
           $image= $_POST["image_name"] ;
           
            //check that the path excsits
           if(file_exists($path)){
            echo "file exists";
            //echo $path;
            unlink($path,$image);
            }else{
              echo"file doesnt exist";
            }

error message: Warning: unlink() expects parameter 2 to be resource, string given in C:\xampp\htdocs\WEBSITE\wwwroot\admin\delete_image.php on line 64

The parth is ok, but I dont think im getting thew right image value to be deleted. I can delete the image manually by

$path = '../../uploads/pic1.jpg';
unlink($parth);

thanks
Ian Wells
<?php
session_start();
if(!isset($_SESSION['agent']) or ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT']))){
require_once('../../secure/connect.php');
require_once('../php/login_functions.php');

        $url = absolute_url();
        header("Location:$url");
        exit();
}

$page_title = 'Delete Product!';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>delete image</title>
</head>
<?php
require_once('../../secure/connect.php');
include ('../includes/header.html');
?>

<!--<link rel="stylesheet" href="css/styleSheet.css" type="text/css" />-->
<body>
<!-- Start of Container -->
<div id="container">
<!--Header -->

<div align="center" >
<?php # delete_image.php

// This page is for deleting a item record.
// This page is accessed through search_product.php

echo '<b><font color="#993300"><h1>Delete Image...</h1></b>';

// Check for a valid user ID, through GET or POST:
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // From view_users.php
	$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form submission.
	$id = $_POST['id'];
} else { // No valid ID, kill the script.
	echo '<p class="error">This page has been accessed in error.</p>';
	//include ('includes/footer.html');
	exit();
}

// Check if the form has been submitted:
if (isset($_POST['submitted'])) {

	if ($_POST['sure'] == 'Yes') { // Delete the record.
           // Get the user's information:
            
           $path = '../../uploads/';
           $image= $_POST["image_name"] ;

           if(file_exists($path)){
            echo "file exists";
            //echo $path;
            unlink($path,$image);
            }else{
              echo"file doesnt exist";
            } 
 
		// Make the query:
		$q = "DELETE FROM images WHERE images.id=$id LIMIT 1";
		$r = @mysqli_query ($mysqli, $q);
                
		if (mysqli_affected_rows($mysqli) == 1) { // If it ran OK.

			// Print a message:
			echo '<p>The image has been deleted.</p>';
                        echo '<p><a href="search_product.php">Return to Admin</a></p>';

		} else { // If the query did not run OK.
			echo '<p class="error">The imjage could not be deleted due to a system error.</p>'; // Public message.
			echo '<p>' . mysqli_error($mysqli) . '<br />Query: ' . $q . '</p>'; // Debugging message.
		}

	} else { // No confirmation of deletion.
		echo '<p>The image has NOT been deleted.</p>';
        echo '<p><a href="search_product.php">Return to Admin</a></p>';
	}

} else { // Show the form.

	// Retrieve the user's information:
	$q = "SELECT * FROM images WHERE id=$id";
	$r = @mysqli_query ($mysqli, $q);

	if (mysqli_num_rows($r) >=0) { // Valid user ID, show the form.

		// Get the user's information:
		$row = mysqli_fetch_array ($r, MYSQLI_NUM);

// Create the form:
	echo '<form action="delete_image.php" method="post">';
        echo '<h3>Name: ' . $row[1] . '</h3>';
        echo "<img src=\"../../uploads/".$row[2]."\" width=\"80px\" /></br>";
	echo '<p>Are you sure you want to delete this user?<br />';
	echo '<input type="radio" name="sure" value="Yes" /> Yes';
	echo '<input type="radio" name="sure" value="No" checked="checked" /> No</p>';
	echo '<p><input type="submit" name="submit" value="Submit" /></p>';
	echo '<input type="hidden" name="submitted" value="TRUE" />';
	echo '<input type="hidden" name="id" value="' . $id .'" />';
        echo '<input type="hidden" name="image_name" value="' .$row[2].'" />';
	echo '</form>';

	} else { // Not a valid user ID.
		echo '<p class="error">This page has been accessed in error.</p>';
	}

} // End of the main submission conditional.

mysqli_close($mysqli);
?>
</div>
</div>
</body>
</html>

Open in new window

0
Comment
Question by:irwells
  • 3
4 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 total points
ID: 33577245
You could simply write:

$image= $_POST["image_name"] ;
$path = '../../uploads/'.$image;
unlink($path);

If this doesn't work try to check  $_POST["image_name"]

Best regards
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33577500
To chekc if you get the right value for image name, first you can add a simple echo $image;exit(); immediatly after having filled $image varible. You could also post your table structure, so I can try to see if row[2] holds image_name. Your code seems to be correct.

Best regards
0
 

Author Comment

by:irwells
ID: 33579220
Thanks marqusG for the quick reply.

I changed

$parth ='../../uploads/';
to
$parth ='../../uploads/'.$image;

and it works great.

Thank you.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33579313
Glad for helped you. Thanks for points.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question