473,396 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Delete image from server using php?

155 100+
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
6 23379
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
DavidPr
155 100+
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
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
DavidPr
155 100+
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
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
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

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

Similar topics

0
by: Silvia | last post by:
Hi, I have a program that capture images and put this into a listview (using imagelist), the problem is when I delete de image the listview, when do that and capture another image, the image...
14
by: php newbie | last post by:
I am getting error messages when I try to delete from a table using the values in the table itself. The intent is to delete all rows from TableA where col_2 matches any of the col_1 values. ...
1
by: Matt Hamilton | last post by:
I have a simple image gallery where I want to allow users to delete files. The problem I have is that after an image is displayed in the browser, I am not able to delete the file because "The...
0
by: Silvia | last post by:
Hi, I have a program that capture images and put this into a listview (using imagelist), the problem is when I delete de image the listview, when do that and capture another image, the image...
0
by: Ed | last post by:
All of a sudden my previously working code started throwing this error. from the SqlDatasource. I am using C# and Asp.net 2.0. Getting the following error: You have specified that your delete...
4
by: Wannabe | last post by:
I am using ASP.Net 2.0 and have a gridview on my page. I have everything working except the delete command. The page reloads except the row I am trying to delete is still there. I believe it is...
0
by: Wannabe | last post by:
I am using ASP.Net 2.0 and have a gridview on my page. I have everything working except the delete command. The page reloads except the row I am trying to delete is still there. I believe it is...
13
by: deppeler | last post by:
I have a script that allows the user to upload images to the server & writes the file name to a flat file DB. I have another script that allows the user to delete the image entry from the DB. Is...
4
by: gerardianlewis | last post by:
Any help appreciated. (VB.NET under XP and Vista, SP1 installed) My code, inherited from a VB6 version of an app that ran under W98, loads an image from a file into a PictureBox. The user may...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.