Page 1 of 1

deleting files

Posted: Fri Oct 27, 2006 11:37 am
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.

Posted: Fri Oct 27, 2006 11:40 am
by akimm
Unlink would do the trick, post the code so we can try to help you..

Posted: Fri Oct 27, 2006 11:51 am
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
}

Posted: Fri Oct 27, 2006 12:17 pm
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.

Posted: Fri Oct 27, 2006 1:22 pm
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.

Posted: Fri Oct 27, 2006 5:40 pm
by RobertGonzalez
That seems like an awfully dangerous function. 8O

Posted: Sun Oct 29, 2006 3:38 pm
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.

Posted: Mon Oct 30, 2006 5:32 pm
by feyd
The code above allows anything to be passed to it.