Jump to content

Is it possible to delete images from directory using php?


AdRock

Recommended Posts

I have created a website which I don't want to have to manage so am trying to make it easy for someone to manage it.

I have an admin area where the database can be managed but I have noticed that when images are no longer used (becuase the are relevant to news articles) it is taking up a lot of room in the images directory.

Is it possible to have a form in the admin area for deleting images from the images directory so I don't have to go to my ftp access to delete unwanted images?
Link to comment
Share on other sites

Yeah, unlink would be much easier. You can do something like

[code]
<?php
$filename = $_POST[filename];

if(file_exists($filename)){
$unlink = unlink($filename);
if($unlink){
echo "File deleted";
}else {
echo "File not deleted";
}
}else {
echo "File does not exist";
};
?>
[/code]
Link to comment
Share on other sites

Thanks, I'll give it a try

Which one of these is right for adding a directory?

[code]    if(file_exists("/images/".$filename))
    if(file_exists("/images/$filename"))[/code]

In my database is records that can be deleted and in the database records are the names of images that are linked to that record.  When I delete the record is it possible to delete the image at the same time using this method or do i have to connect to the database to pull the record out using mysql_fetch_array?

It would be very handy to delete the image the same time as the record from the database

Here is the code that I have which actualy deletes the record

<h2>Delete a general article</h2>
[code]<?php
    include_once("../includes/connection.php");

    $id=$_GET['id'];
    $title=$_POST['title'];
    $content=$_POST['content'];
    $filename = $_POST[filename];

    $query = "DELETE FROM articles WHERE id='$id'";
    mysql_query($query);

    if(file_exists("/images/".$filename)){
    $unlink = unlink("/images/".$filename);
if($unlink){
    echo "File deleted";
}else {
    echo "File not deleted";
}
    }
    mysql_close();
?>
<p class="style3">Thankyou. News item successfully deleted.</p>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.