I upload pictures and allow users to log in and view them for 30 days using the following statement

SELECT * FROM pictures WHERE date >= DATE_SUB(NOW(), INTERVAL 30 DAY) and email='$session_email

When i upload a picture for a user the name is stored as a field in a table of my database as:

image.jpg

I need a way to delete ALL images from the directory it is in (which is always "/var/www/login/upload/uploads") that are older than 30 days. So I was thinking something like this:

$sql ="SELECT * FROM pictures WHERE created <= DATE_SUB(NOW(), INTERVAL 30 DAY)";
mysql_query($sql);

while ($row=mysql_fetch_assoc($sql)){
//THE FOLLOWING LINE IS WHAT I DON'T KNOW HOW TO DO.
DETELE FILE "/var/www/login/upload/uploads/".$row['file'].""
}

//THEN I NEED TO RUN ANOTHER SQL STATEMENT TO REMOVE THE ENTRIES IN THE DB:
DELETE FROM pictures WHERE date <= DATE_SUB(NOW(), INTERVAL 30 DAY)

I know I have the right idea, I just don't know the correct syntax to perform the deletion of the actually physical files from the uploads directory.

I plan on running this with a cron every night.

Any help is greatly appreciated.

Recommended Answers

All 2 Replies

Thanks! unlink() was exactly what i was looking for!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.