By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

Delete image from server using php?

100+
P: 155
I made a little picture gallery for my website. The images and thumbnails are stored on the server and the information in a database. I know how to delete the information from the databasem, but how would I include instructions in the php script to also remove the thumb and image from the server?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if($_GET["cmd"]=="delete")
  3. {
  4.     $sql = "DELETE FROM $table WHERE id=$id";
  5.  
  6. // code to delete images from server
  7.  
  8.     $result = mysql_query($sql);
  9.  
  10. echo "<br><br><p align='center'><span style='color:red'><b>The Image and Information has been Deleted!<br><br>";
  11. }
  12. ?>
Jun 19 '07 #1
Share this Question
Share on Google+
6 Replies


P: 4
Before deleting the RECORD. Delete the files
try the unlink()
delete() functions ...
i hope you have the file locations stored on the database

fetch it delete the files
delete the record.
Jun 20 '07 #2

100+
P: 155
No, I don't have the location of the image files stored in the database? Is this a good idea? If so, how would I show or store the path to images? I'd have a field named img_url and have the location like this: (/home/pictures/image_uploads/)

To unlink a file I would use this: [PHP]unlink("$img_url/tree.jpg");[/PHP]

How would I tie this in with a delete script that removes the information regarding this image from the database?
Jun 20 '07 #3

bonski
P: 53
No, I don't have the location of the image files stored in the database? Is this a good idea? If so, how would I show or store the path to images? I'd have a field named img_url and have the location like this: (/home/pictures/image_uploads/)

To unlink a file I would use this: [PHP]unlink("$img_url/tree.jpg");[/PHP]

How would I tie this in with a delete script that removes the information regarding this image from the database?

ei david,

it doesn't matter if you dont have you directory paths.. as long as you know where your images are located... and most importanty the image filename should be save in the database..

ok... how you would tie it together with delete script.. heres how..

Expand|Select|Wrap|Line Numbers
  1. $img_dir = 'image_directory_name/';
  2. $img_thmb = 'thumbnail_directory_name/';// if you had thumbnails
  3.  
  4. $image_name = $row['image_name'];//assume that this is the image_name field from your database
  5.  
  6. //unlink function return bool so you can use it as conditon
  7. if(unlink($img_dir.$image_name) && unlink($img_thmb.$image_name)){
  8.     //assume that variable $image_id is queried from the database where your image record your about to delete is...
  9.     $sql = "DELETE FROM table WHERE image_id = '".$image_id."'";
  10.     $qry = mysql_query($sql);
  11. }else{
  12.    echo 'ERROR: unable to delete image file!';
  13. }
ok? have fun... ^___^

bonski
Jun 20 '07 #4

100+
P: 155
Thank you for the example.

Expand|Select|Wrap|Line Numbers
  1. $sql = "DELETE FROM table WHERE image_id = '".$image_id."'";
May I ask why you placed the image_id like this'".$image_id."'instead of like this '$image_name'?
Jun 20 '07 #5

bonski
P: 53
Thank you for the example.

Expand|Select|Wrap|Line Numbers
  1. $sql = "DELETE FROM table WHERE image_id = '".$image_id."'";
May I ask why you placed the image_id like this'".$image_id."'instead of like this '$image_name'?
oh... well i'm just used to do it like that... its a concatenation... notice the dot(.)

well, i guess... they are just the same.. im just doing it like that so that its easy for me to trace... ^___^

ok... your welcome...!
Jun 20 '07 #6

P: 1
Here's the code that I'm trying to use from this post..

Expand|Select|Wrap|Line Numbers
  1. <?php include('dbconnect.php');
  2.  
  3. $img_dir = '../images/ads/';
  4. $img_thmb = '../images/ads/sml_';
  5.  
  6. $ad_photo = $row['ad_photo'];
  7.  
  8. //unlink function return bool so you can use it as conditon
  9. if(@unlink($img_dir.$ad_photo) && @unlink($img_thmb.$ad_photo)){
  10. $sql = "DELETE FROM pbsl_ads WHERE ad_id = '".$ad_id."'";
  11. $qry = mysql_query($sql);
  12. }else{
  13. echo 'ERROR: unable to delete image file!, <a href=index.php> back</a>';
  14. }
  15.  
  16. ?>
The image wont delete from the server. I'm able to delete info from database.

This is what I have in my db table:

Expand|Select|Wrap|Line Numbers
  1. `ad_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  2. `ad_date` VARCHAR(60) NOT NULL,
  3. `ad_order` INT(3) NOT NULL,
  4. `ad_name` TEXT NOT NULL,                     // this is the name of the person
  5. `ad_info` TEXT NOT NULL,
  6. `ad_email` TEXT NOT NULL,
  7. `ad_photo` VARCHAR(50) NOT NULL,       // this is the photo
  8. `ad_cardphoto` VARCHAR(50) NOT NULL  // this is the business card photo
Thanks for any help
Jan 1 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.