Question

I'm trying to write a script to upload a script to remove an image from my server. I keep getting the error message. Can anyone find anything wrong with this code?

// Delete image
if(isset($_GET['deleteImg']) && !empty($_GET['deleteImg']) && $_GET['deleteImg'] == true)
{
 // Get imagepath from database
 $result = mysql_query("SELECT image FROM frankkluytmans WHERE id=$id");
 $imageDeletePath = mysql_fetch_assoc($result);

 // Delete image from server
 if(unlink($imageDeletePath['image']))
 {
    // Continue if image has been reset in database
    if(mysql_query("UPDATE frankkluytmans SET `image`='' WHERE id=$id")){
        // once deleted, redirect back to the view page
        header("Location: index.php"); 
    }
 }
 else
 {
    ?>
    <script type="text/javascript">
        window.alert('This image could not be deleted.';
    </script>
    <?
 }
}
Was it helpful?

Solution

I guess your error is in the path of your unlink() function, as you said actual field is like /gfx/image.png wich doesn't look to me like an absolute path, correct me if i'm wrong.

To delete the file and use unlink() directly your script should be in the same folder as the image. So i think it would be better to set an absolute path to your entry like

$path_abs = ' /customers/d/8/e/frankkluytmans.nl/httpd.www/testsite/cms'; //is the `gfx` folder inside `cms` folder? if it is this will work otherwise you have to change
if(unlink($path_abs . $imageDeletePath['image']))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top