deleting files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sh33p1985
Forum Commoner
Posts: 78
Joined: Thu Mar 11, 2004 9:22 am

deleting files

Post by sh33p1985 »

my product records have an associated field which points its product image (which was uploaded on the creation of the product record). now i have an edit form to change any of the initial values set upon creation of the record, which also has the option of changing the image. this part is simple enough with an upload form BUT it would be silly to not deal with the redundant image that was replaced as it is just going to waste server space. i would like to know if there is a way to remove that image following a successful edit (the path to image is known).

i have been looking at the file unlink function.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Unlink would do the trick, post the code so we can try to help you..
sh33p1985
Forum Commoner
Posts: 78
Joined: Thu Mar 11, 2004 9:22 am

Post by sh33p1985 »

Code: Select all

if(move_uploaded_file($_FILES['newImageUpload']['tmp_name'], $target_path)){		
//remove old file
$oldImage = $_POST['imageURL'];
unlink($oldImage);
}
else{
//upload failed
}
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

First off, what does it do when you use that code there? Secondly, you want want to take a newly uploaded image and replace it with the one that was in its place? Are you using a mySQL db I presume.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Be extremely careful with the usage of unlink() you have there ~sh33p1985. A malicious user could request that your script erase any file on your server.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

That seems like an awfully dangerous function. 8O
sh33p1985
Forum Commoner
Posts: 78
Joined: Thu Mar 11, 2004 9:22 am

Post by sh33p1985 »

security measures are in place the area is protected only one person will have access to the changing the images. and ofc the parameter passed to unlink will be the old image, no way anything else will be able to passed to it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The code above allows anything to be passed to it.
Post Reply