Jump to content

Deleteing Image


arunpatal

Recommended Posts

This code delete the image which is saved as id number:

 

Example

 

In my table there are 4 fields

 

id (ai and pk)

name

image1

image2

 

Now if my id is 137 and the name of image also 137.jpg

then the script delete the image...

I want to delete images from images folder and name of these images are not saved as id but in image1 and image2

 

 

<?php
// Delete Item Question to Admin, and Delete Product if they choose
if (isset($_GET['deleteid'])) {
echo 'Do you really want to delete product with ID of ' . $_GET['deleteid'] . '? <a href="list.php?yesdelete=' . $_GET['deleteid'] . '">Yes</a> | <a href="list.php">No</a>';
exit();
}
if (isset($_GET['yesdelete'])) {
// remove item from system and delete its picture
// delete from database
$id_to_delete = $_GET['yesdelete'];
$sql = mysql_query("DELETE FROM test WHERE id='$id_to_delete' LIMIT 1") or die (mysql_error());
// unlink the image from server
// Remove The Pic -------------------------------------------
$pictodelete = ("images/$id_to_delete.jpg");
if (file_exists($pictodelete)) {
		 unlink($pictodelete);
}
header("location: list.php");
exit();
}
?>
<table>
<tr>
<td>Name</td>
<td>Action</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_list['name']; ?></td>
<td> <a href='list.php?deleteid=<?php echo $row_list['id']; ?>'>delete</a></td>
</tr>
<?php } while ($row_list = mysql_fetch_assoc($list)); ?>
</table>

Link to comment
https://forums.phpfreaks.com/topic/273267-deleteing-image/
Share on other sites

Retrieve the value from those two fields, before you delete the row from the database. Then, after successful deletion of the row, check for and delete the images.

 

I don't understand

 

the value (name of image) of image shows like this

$row_list['image']

$row_list['image1']

 

What should i do?

Link to comment
https://forums.phpfreaks.com/topic/273267-deleteing-image/#findComment-1406369
Share on other sites

I am trying to delete the image

 

This code is very simple

 

<?php

if (isset($_GET['delete']))
$id_to_delete = $_GET['delete'];

$pictodelete = ("images/$id_to_delete");
   if (file_exists($pictodelete)) {
		 unlink($pictodelete);
   }
?>

 

Here is the delete link code

 

<a href='list.php?delete=<?php echo $row_list['image_2']; ?>'>delete</a>

 

But the problem is that i want to delete 4 images which are in same row

$row_list['image_1']

$row_list['image_2']

$row_list['image_3']

$row_list['image_4']

 

I tried like this

<a href='list.php?delete=<?php echo $row_list['image_1']; ?>&<?php echo $row_list['image_2']; ?>'>delete</a>

But its only deleting $row_list['image_2']

 

How can i delete all these images at once ???????

Link to comment
https://forums.phpfreaks.com/topic/273267-deleteing-image/#findComment-1406524
Share on other sites

You don't delete them all at once, you delete them one after another. You already have a snippet that deletes one image, all you need to do now is to make it loop through the images from the row from the database, and delete the filenames from it.

 

If you want to learn how to program, this is something you need to learn how to figure out on your own. Writing code is just a (small) part of programming, with the most of the work going into the planning phase before you start to write the first line of code. Take your time to properly plan out your code first, and solve all of the logic in that step, and writing code becomes a simple matter of translating from English to PHP.

A lot easier than trying to solve the problem and translate it at the same time. ;)

Link to comment
https://forums.phpfreaks.com/topic/273267-deleteing-image/#findComment-1406651
Share on other sites

You don't delete them all at once, you delete them one after another. You already have a snippet that deletes one image, all you need to do now is to make it loop through the images from the row from the database, and delete the filenames from it.

 

If you want to learn how to program, this is something you need to learn how to figure out on your own. Writing code is just a (small) part of programming, with the most of the work going into the planning phase before you start to write the first line of code. Take your time to properly plan out your code first, and solve all of the logic in that step, and writing code becomes a simple matter of translating from English to PHP.

A lot easier than trying to solve the problem and translate it at the same time. ;)

 

Your are right..... It will take much more time then asking for help but i will be learning more...

I will try myself and hopefully will figer out how can i delete these images.

 

Thanks for nice advice and also help :)

Link to comment
https://forums.phpfreaks.com/topic/273267-deleteing-image/#findComment-1406681
Share on other sites

You're welcome, glad I could help. :)

 

Also, after you've solved it, it's always possible to come back here and ask for feedback on the code you wrote. That way you can learn even more. ;)

 

Surly, i will come back within 24hours from now and hopefully with nice results :)

Link to comment
https://forums.phpfreaks.com/topic/273267-deleteing-image/#findComment-1406784
Share on other sites

Hi, i wanted to do this thing via loop but its very hard for me to understand.....

 

But the way i make is also working.....

I am passing 5 values via delete link

 

<?php
if (isset($_GET ['deleteid'])) {
$id = $_GET ['deleteid'];
$pic1 = $_GET ['deleteimg1'];
$pic2 = $_GET ['deleteimg2'];
$pic3 = $_GET ['deleteimg3'];
$pic4 = $_GET ['deleteimg4'];


$pictodelete1 =("images/$pic1");
$pictodelete2 =("images/$pic2");
$pictodelete3 =("images/$pic3");
$pictodelete4 =("images/$pic4");

if (file_exists($pictodelete1)) {
 	 unlink($pictodelete1);
unlink($pictodelete2);
unlink($pictodelete3);
unlink($pictodelete4);
}

$sql = mysql_query("DELETE FROM test WHERE id='$id' LIMIT 1") or die (mysql_error());

};
?>

 

 

But am eager to know that how will this function with the help of loop....... :confused: :confused: :confused: :confused:

Link to comment
https://forums.phpfreaks.com/topic/273267-deleteing-image/#findComment-1406825
Share on other sites

Hi,

 

I am trying to loop like this

 

<?php
if(isset($_POST['image'])) {
for ($i=0;$i<count($_POST['image']);$i++) {


$pictodelete = ("images/$_POST['image'][$i]");
}
}
?>

 

But images/$_POST['image'][$i] gives me error

 

i am sending these values

 

<form method='POST' action="list.php">
<input type="text" name="image[]" value="<?php echo $row_list['id']; ?>" />
<input type="text" name="image[]" value="<?php echo $row_list['image_1']; ?>" />
<input type="text" name="image[]" value="<?php echo $row_list['image_2']; ?>" />
<input type="text" name="image[]" value="<?php echo $row_list['image_3']; ?>" />
<input type="text" name="image[]" value="<?php echo $row_list['image_4']; ?>" />
<input type="submit" value="Delete" />
</form>

Link to comment
https://forums.phpfreaks.com/topic/273267-deleteing-image/#findComment-1406922
Share on other sites

Finally i come up with this code.......

I think this is the best way. Please let be know if it can be written better :happy-04:

 

<?php
if(isset($_POST['image'])) {
$del = array ($_POST['image'][0], $_POST['image'][1], $_POST['image'][2], $_POST['image'][3], $_POST['image'][4]);
for ($i=0;$i<count($del);$i++) {
$pictodelete = ("images/$del[$i]");
if (file_exists($pictodelete)) {
unlink($pictodelete);
}
}}
?>

 

$_POST['image'] is already array and i tried the code below but it gives me error in line images/$_POST['image'][$i]

 

<?php
if(isset($_POST['image'])) {

for ($i=0;$i<count($_POST['image']);$i++) {
$pictodelete = ("images/$_POST['image'][$i]");

if (file_exists($pictodelete)) {
unlink($pictodelete);
}
}}

?>

Link to comment
https://forums.phpfreaks.com/topic/273267-deleteing-image/#findComment-1406960
Share on other sites

OK so this is how it works

 

This form send values

<form method='POST' action="list.php">
<input type="hidden" name="image[]" value="<?php echo $row_list['id']; ?>" />
<input type="hidden" name="image[]" value="<?php echo $row_list['image_1']; ?>" />
<input type="hidden" name="image[]" value="<?php echo $row_list['image_2']; ?>" />
<input type="hidden" name="image[]" value="<?php echo $row_list['image_3']; ?>" />
<input type="hidden" name="image[]" value="<?php echo $row_list['image_4']; ?>" />
<input type="submit" value="Delete" />
</form>

 

This code deletes the all images + id row from database

 

<?php
if(isset($_POST['image'])) {

for ($i=0;$i<count($_POST['image']);$i++) {

$pictodelete = ("images/".$_POST['image'][$i]."");

if (file_exists($pictodelete)) {
unlink($pictodelete);
}

$sql = mysql_query("DELETE FROM test WHERE id='".$_POST['image'][$i]."' LIMIT 1") or die (mysql_error());

}}
?>

Link to comment
https://forums.phpfreaks.com/topic/273267-deleteing-image/#findComment-1406961
Share on other sites

Your first attempt with the loop was actually very close to getting it right. The only thing that stopped yours from working, was that you should have used the complex syntax for adding an array value inside the string. Or closed the string and concatenated the variable content.

Using a foreach () would also have been a bit better, since it would reduce the complexity of your code a bit.

 

if (isset ($_POST['image'])) {
   foreach ($_POST['image'] as $image) {
       // TODO: Validate the image name, to ensure it contains only characters legal for a filename.
       $pictodelete = ("images/".$image);

       // TODO: Check if the file exists, and actually delete it.
   }
}

 

Note that this code is open for attacks, with which a malicious user can delete any arbitrary files from your server. That's why you need to verify that the characters in the given filename are only characters legal to use in a filename, and that there are no way it can be used to traverse the directory tree.

Link to comment
https://forums.phpfreaks.com/topic/273267-deleteing-image/#findComment-1407127
Share on other sites

Your first attempt with the loop was actually very close to getting it right. The only thing that stopped yours from working, was that you should have used the complex syntax for adding an array value inside the string. Or closed the string and concatenated the variable content.

Using a foreach () would also have been a bit better, since it would reduce the complexity of your code a bit.

 

if (isset ($_POST['image'])) {
foreach ($_POST['image'] as $image) {
// TODO: Validate the image name, to ensure it contains only characters legal for a filename.
$pictodelete = ("images/".$image);

// TODO: Check if the file exists, and actually delete it.
}
}

 

Note that this code is open for attacks, with which a malicious user can delete any arbitrary files from your server. That's why you need to verify that the characters in the given filename are only characters legal to use in a filename, and that there are no way it can be used to traverse the directory tree.

 

Got it.. :happy-04:

Thanks

Link to comment
https://forums.phpfreaks.com/topic/273267-deleteing-image/#findComment-1407349
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.