Delete an image using PHP?

Johnies

New member
I have a folder images/ i've created a php script that uploads the image and now i want to delete it. I have in the form that deletes the image a <input type="text" name="img_del" value='".$row[image]."'> that takes a value of the image's path to be deleted. I've tried the unlink function but still nothing. I was thinking of writing sth like unlink($_POST[img_del]); HELP???
 
First add the next function to your file:
function delete_images_by_refnum($image_folder, $ref_num)
{
$d = opendir($image_folder);'
if(!is_resource($d)) return;
while($f = readdir($d))
{
if(substr($f, 0, strlen($ref_num)+1) == $ref_num + '_')
unlink ($image_folder.'/'.$f);
}
}

Secondly edit the code you quotes in your last post to call the function:

if ($row = mysql_fetch_array($PropertiesResult))
{
$ref_num = $row['property_ref_num'];
delete_images_by_refnum(dirname(__FILE__).'/myimagefolder', $ref_num);

You have to change the first argument to state the correct foldername offcourse. You have to provide a relative link to the file this code is in. So when your code is in lib/delete.php and your images are in img/ the command would be:
delete_images_by_refnum(dirname(__FILE__).'/../img', $ref_num);
 
Back
Top