proper use of unlink() for deleting record and image

Posted on 2005-05-09
Last Modified: 2013-12-12 there anyone who can show me the proper code for using unlink() ?  I have a table in the database with image path on it.

so it looks like this

listingID  | listingTitle | desc                        | image                  | date
1            |cheap psx  | good and cheap psx  | c:/images/psx.jpg | 8/5/2005

I wanted to delete this listing  once it get past its expiry date(30 days from the listing) The problem is the image would still be in the database...I have read a thing or two about unlink...but i'm not sure of its proper usage.

I am using php, mysql, apache and windows 2000. Thank you.

Question by:spradjinata
    LVL 6

    Expert Comment

    Hi spradjinata,

    You have a full path in your database. That's kinda good. Not portable, but good for your case.
    First, you get all of your "expired" images list.
    $sql = 'SELECT * FROM table WHERE NOW() > DATE_ADD(`date`, INTERVAL 30 DAY)';
    // assuming your `date` column is of DATETIME type
    $result = mysql_query($sql);
    while ($row = mysql_fetch_assoc($result)) {
       $deleteSuccessful = @unlink($row['image']); // delete an image
       if ($deleteSuccessful) { // if delete successfull
            $sql = 'DELETE FROM table WHERE image = "'.$row['image'].'" AND listingID = "'.$row['listingID'].'"';
            mysql_query($sql); // delete record from database

    German Rumm.
    LVL 32

    Accepted Solution

    Do you want to delete the record or just the file ?

    // DB settings
    $result = mysql_query("SELECT * FROM table_name WHERE CURDATE() > DATE_ADD(date, INTERVAL 30 DAY)") or die("SQl error: " . mysql_error());
    while($row = mysql_fetch_array($result)) {
      $filename = $row["image"];
      // Delete the file.
      if(file_exists($filename)) {
      $records[] = $row['listingID'];
    // Delete the records in single query
    if(isset($records) && !empty($records)) {
     $records_to_del = implode(',', $records);
     mysql_query("DELETE FROM table_name WHERE listingID IN ( $records_to_del )") or die("Delete query error: " . mysql_error());
     echo mysql_affected_rows() . ' records deleted.';

    Author Comment

    I want to delete both the record and the image. both solution looks great. I will award the points after I tried it. Thanks very much.

    Author Comment

    thank you ldbkutty....your answer is perfect...thanks.....sorry german rumm...your answer doesn't work...i wanted to give you points but i can't split it....(the split point is missing)......thanks though.
    LVL 32

    Expert Comment

    >> thank you ldbkutty....your answer is perfect...thanks.....

    >> (the split point is missing)

    If you wish, I can reopen the question or split the points.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now