473,568 Members | 2,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete image from server using php?

155 New Member
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 23390
digitalpbk
4 New Member
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 New Member
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_ur l/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 New Member
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_ur l/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 New Member
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 New Member
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
jayrama
1 New Member
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
1914
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 painting in the listview is the image delete, I supose that the problem is that the image exist in the imagelist, in order to solve this problem I...
14
3597
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. DELETE FROM TableA FROM TableA x INNER JOIN TableA y ON (x.col_1 = y.col_2) Error msg: The table 'TableA' is ambiguous. Can this be done with SQL...
1
1877
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 process cannot access the file ... It is being used by another process". I also get this error when trying to delete through explorer on the server. I...
0
1812
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 painting in the listview is the image delete, I supose that the problem is that the image exist in the imagelist, in order to solve this problem I...
0
3316
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 command compares all values on SqlDataSource 'sdsAddEdit', but the dictionary passed in for values is empty. Pass in a valid dictionary for delete...
4
2594
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 something really easy, but I cannot see it. The stored procedue works when run in QA. Can someone tell me what I am doing wrong/missing that is...
0
1775
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 something really easy, but I cannot see it. The stored procedue works when run in QA. Can someone tell me what I am doing wrong/missing that is...
13
1745
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 there a way to also delete the image from the server folder as well? Here is the script I am using to delete the entry in the DB: if...
4
5556
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 want to move or delete the file that is being shown. The corresponding event- driven code for "delete this picture" uses: ...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6277
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.