473,387 Members | 1,621 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,387 software developers and data experts.

Deleting uploaded images through php.

Markus
6,050 Expert 4TB
Now, i know it's possible to delete files using 'unlink'.

But, i tried this against a jpg file, to no avail.

I have done some research but have been unable to find anything to approach this with.

Sorry in advance.

And thanks... in advance :D
Oct 31 '07 #1
6 1370
Atli
5,058 Expert 4TB
Hi Markus.

The unlink() function can delete all files on your server it has access to. That is to say, the user running the PHP process must have write permission on the file to delete it.

Could you show us your code and any errors you were getting?
Oct 31 '07 #2
Markus
6,050 Expert 4TB
Yeh sure!

I used the 'unlink' on an image BUT i checked and the image was still there..

Here's the code :)
[php]
<?php
$file = $_GET['file']; //file to delete

include("../connectionstuff"); //config for mysql
include("../connectionstuff"); //open database mysql

$query = "DELETE FROM `ma_pics` WHERE `url_short` = '$file'"; /*delete from database! If not done, images will try to appear in the recently uploaded section! */
mysql_query($query) or die ("Error encountered: ".mysql_error.""); // query it.

$fh = fopen($file, 'w') or die("can't open file"); // open file
fclose($fh); // and close so no other part is using it!
unlink($file) or die("error!"); //this is the deleting part :D
echo "<p>Image '".$file."': deleted.</p>"; //on success => let user know of delete
?>
[/php]

I don't get any errors, just that the file is never ACTUALLY deleted.

Thanks :)
Oct 31 '07 #3
Atli
5,058 Expert 4TB
Ok, I see no major problem with that code. If the $_GET super-global contains a valid file path then that file should be deleted. (Assuming the database query doesn't fail and stop the code first.)

What does the file path look like? Are you sure it points to a the file?

A few notes about the code.
You include the "../connectionstuff" twice. Dunno if thats on purpose or not but, anyways.
Also, assuming you use the fopen() and fclose() functions there on the $file path to check if it actually exists, you could try the file_exists() function or even the is_writable() function to see if it exists and can be deleted.
Oct 31 '07 #4
Markus
6,050 Expert 4TB
Ok, I see no major problem with that code. If the $_GET super-global contains a valid file path then that file should be deleted. (Assuming the database query doesn't fail and stop the code first.)

What does the file path look like? Are you sure it points to a the file?

A few notes about the code.
You include the "../connectionstuff" twice. Dunno if thats on purpose or not but, anyways.
Also, assuming you use the fopen() and fclose() functions there on the $file path to check if it actually exists, you could try the file_exists() function or even the is_writable() function to see if it exists and can be deleted.
Thanks for the input!

the connection stuff was actually two seperate things but i removed the actual names of the files :)

I'll have a play around with stuff.

I've hit a new problem though with creating directories, but thats for another time, i think.

Thanks a bunch.
Oct 31 '07 #5
Markus
6,050 Expert 4TB
The mysql query is working fine: the url is deleted from the db

just the actual image isnt being deleted ¬_¬
Oct 31 '07 #6
Markus
6,050 Expert 4TB
Would this(script), by any chance, have to be in the same directory as the file being deleted?

SOLVED!!!!
Oct 31 '07 #7

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

Similar topics

1
by: Mat | last post by:
Hi, I have a system that uploads images as BLOBs in a database. I also have a function that I use to resize uploaded images before saving as files. I would like to combine these two by resising...
8
by: phpfrizzle | last post by:
Hi there, I have built a CMS, and in one part you have to be able to delete images. Structure like this: _root --> folder "img" --> file "1.jpg" --> file "2.jpg" --> folder "CMS" ...
1
by: Eddie Tan | last post by:
Hi everyone hope you guys can help me out on this. I'm working on an upload page where it has the file name, file size, and type of the file that is uploaded. I want to add a checkbox next to each...
2
by: ed | last post by:
Hi everyone hope you guys can help me out on this. I'm working on an upload page where it has the file name, file size, and type of the file that is uploaded. I want to add a checkbox next to each...
3
by: troy | last post by:
Hello, I have the following code that run in the page_load section: private void Page_Load(object sender, System.EventArgs e) { /// Create uploads folder in the Virtua; directory if one...
4
by: greyseal | last post by:
I've gone around and around this thing for the last day or so. Using WIA, I can take and copy images from my Nikon D2H. However, when I attempt to delete the image I find that that capability is...
0
by: Satish Appasani | last post by:
Hi: I have a ASP.NET form with Web layout which I've achieved using panels. In one of the tab I have a File control to upload Images. When I put a file in the file control and move to another...
5
by: IkBenHet | last post by:
Hello, I use this script to upload image files to a folder on a IIS6 server: ******************* START UPLOAD.ASPX FILE ********************** <%@ Page Language="VB" Debug="true" %>
8
by: ctiggerf | last post by:
I was hopeing someone could help me out here. Been stumped on this one all day. This function 1. Checks uploaded files. 2. Creates two resized images from each (a full size, and a...
1
by: shafshaa | last post by:
Hi i am working on a website, something sort of similar to myspace idea, users can upload images, the images are uploaded to a folder on the server, i have managed to get that bit ok, i am just...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
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...
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...

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.