Current location: Hot Scripts Forums » Programming Languages » PHP » How to delete the uploaded images


How to delete the uploaded images

Reply
  #1 (permalink)  
Old 03-05-07, 12:04 PM
itssami itssami is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
How to delete the uploaded images

i wrote some code to upload the pictures to a folder and after it has uploaded, there's a page, say pics.php.. it displays all pictures of the folder to that page. now i want to add an option to delete the pictures. for example i want to add a link "Delete" under every pic, that when i click it deletes the pic from the folder and deletes the pic data from mysql.
How i can delete the pic from the folder? thanks
Reply With Quote
  #2 (permalink)  
Old 03-05-07, 12:13 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
You can delete files with unlink(). But be careful. Make sure the file name passed in the URL has an image extension, and make sure the user isn't able to insert something like ?delete=../pics.php. Use basename() on the value which you receive in the URL.

This should work. (Untested)
PHP Code:



if (isset($_GET['delete']) AND preg_match('/\.(jpe?g|bmp|png|gif)$/i'$_GET['delete']))
{
    @
unlink('./path/to/images/'rawurldecode(basename($_GET['delete'])));


HTML Code:
<a href="foo.php?delete=image.jpg">Delete</a>
Reply With Quote
  #3 (permalink)  
Old 03-05-07, 12:26 PM
Bon Bon Bon Bon is offline
Newbie Coder
 
Join Date: Mar 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
It should also be noted that you should restrict this feature in some form like user authentication for instance to avoid this script being used for malicious purposes.
Reply With Quote
  #4 (permalink)  
Old 03-05-07, 01:05 PM
itssami itssami is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
but does it delete the images in the folder? or it just unlinks the images in mysql? i want to delete the image from folder as well, to save the space.
Reply With Quote
  #5 (permalink)  
Old 03-05-07, 01:34 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
unlink() pertains to PHP, not MySQL. That code deletes files from the file system.
__________________
The toxic ZCE
Reply With Quote
  #6 (permalink)  
Old 03-05-07, 01:35 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
EDIT: A little bit too late...

This would only delete the image in the folder. I can't give you an exact code for the database task since I don't know your structure.

But something like this should work:

PHP Code:


if (isset($_GET['delete']) AND preg_match('/\.(jpe?g|bmp|png|gif)$/i'$_GET['delete']))
{
    
$image rawurldecode(basename($_GET['delete']));
    
   
// Delete from folder
   
@unlink('./path/to/images/'$image);

    
// Delete from database
    
mysql_query("DELETE FROM your_table WHERE imagename = '"mysql_real_escape_string($image) ."' LIMIT 1") OR die( mysql_error() );

Reply With Quote
  #7 (permalink)  
Old 03-05-07, 01:58 PM
itssami itssami is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Nico, thats what im looking for.. but im facing a problem in above code. It deletes the image from folder.. but on secong query, it gives the following error

Fatal error: Call to undefined function: mysql_real_escape_string() in

and when i try to remove that function, and write query like
DELETE FROM your_table WHERE imagename = '$image'
then it works..
can someone tell me whats wrong with that mysql_real_escape_string function ? is it syntax error or something else. thanks again.
Reply With Quote
  #8 (permalink)  
Old 03-05-07, 02:02 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
With "it works", do you mean the row is being deleted from the database after running the code? Are you running a PHP version below 4.3.0? If so, try replacing mysql_real_escape_string with mysql_escape_string. If that still doesn't work, can you may post the code of the upload script?
Reply With Quote
  #9 (permalink)  
Old 03-05-07, 02:21 PM
itssami itssami is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
it works with mysql_escape_string (it deletes the records from mysql) , and about the php version, actually i insatelled PHPTraid on my computer to practice php. i wanted to install php and mysql etc but i couldnt configure phpmyadmin. anyway thank you very very much for help.
Reply With Quote
  #10 (permalink)  
Old 03-05-07, 02:24 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
If mysql_escape_string works, then are you probably running an old version. You can see that if you do a phpinfo() or phpversion() statement.

For learning purposes, I'd recommend XAMPP. It's very easy to install, and comes with everything you need.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Check uploaded files size zoliky PHP 5 09-27-06 01:52 PM
image delete problem mys10 ASP 1 03-31-05 08:46 AM
images uploaded via php upload come out distorted Ronald PHP 0 03-09-05 01:19 PM
need help with viewing uploaded image on webpage? mikewooten PHP 0 03-16-04 07:39 PM
i know how to delete a file! but how to delete a directory? forcer JavaScript 3 01-28-04 07:00 AM


All times are GMT -5. The time now is 05:57 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.