Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete unused images from root folder

Status
Not open for further replies.

lagc

Programmer
Jan 21, 2009
79
GB
Hi all again, hope everybody is well.

I have just had an email from our host letting me know that we are getting very close to our capacity, and we need to trim things down a bit.

My biggest problem folder is our image folder, as it contains about 10 years worth of hotel images, and probably 50% are now unused.

So what I'm looking for is a script that checks the database and if its not in the database, it is deleted from the image folder. There are 4 folder tables, and one image folder at the root level.

Any ideas anybody.

Cheers
 
Hi

First of all, I prefer a shell script for a such administrative tasks.

Probably the simplest approach would be this :
[ul]
[li]create a temporary directory[/li]
[li]move all images to a temporary directory[/li]
[li]for each image table[ul]
[li]select image names from the table[/li]
[li]for each image name[ul]
[li]move the image back to the image directory[/li][/ul][/li][/ul][/li]
[li]delete the images in the temporary directory[/li]
[li]delete the temporary directory[/li]
[/ul]
Tell us if you hit a problem implementing it.

Feherke.
 
Hi feherke,

I will be honest and say that I've hit a snag already as I'm not a solid php programmer, I can change and read through scripts, but creating something liek this from scratch, is a bit above me I'm affraid.

If you know of any examples that I can read that would be great, if not at least you have given me the start I need.

Cheers
 
Hi,

Yes sure, we are using a MySQL database, and each image table is named as follows.

Code:
Foto1_Hot 
Foto2_Hot 
Foto3_Hot 
Foto4_Hot

Type is VARCHAR

Each image in those tabels is displayed as follows:

Code:
/imgdata/hotel/8349otel_16_by.jpg

What else are you looking for feherke? I have cpanel open, and can quickly get any information you need.

Cheers
 
Hi

You told us the table names but forgot the field names. So I supposed it is imagename.
I supposed the temporary directory you created and where you moved the image files is tempdir/.
I supposed that the /imgdata/hotel/ URI is relative path from the DocumentRoot.
PHP:
[COLOR=darkred]<?php[/color]

[COLOR=darkgreen]$con[/color][COLOR=darkred]=[/color][b][COLOR=black]mysql_connect[/color][/b][COLOR=darkred]([/color][COLOR=red]'hostname'[/color][COLOR=darkred],[/color][COLOR=red]'username'[/color][COLOR=darkred],[/color][COLOR=red]'password'[/color][COLOR=darkred]);[/color]
[b][COLOR=black]mysql_select_db[/color][/b][COLOR=darkred]([/color][COLOR=red]'database'[/color][COLOR=darkred]);[/color]

[b][COLOR=blue]foreach[/color][/b] [COLOR=darkred]([/color][b][COLOR=blue]array[/color][/b][COLOR=darkred]([/color][COLOR=red]'Foto1_Hot'[/color][COLOR=darkred],[/color][COLOR=red]'Foto2_Hot'[/color][COLOR=darkred],[/color][COLOR=red]'Foto3_Hot'[/color][COLOR=darkred],[/color][COLOR=red]'Foto4_Hot'[/color][COLOR=darkred])[/color] [b][COLOR=blue]as[/color][/b] [COLOR=darkgreen]$table[/color][COLOR=darkred])[/color] [COLOR=red]{[/color]
  [COLOR=darkgreen]$res[/color][COLOR=darkred]=[/color][b][COLOR=black]mysql_query[/color][/b][COLOR=darkred]([/color][COLOR=red]"select imagename from $table"[/color][COLOR=darkred]);[/color]
  [b][COLOR=blue]while[/color][/b] [COLOR=darkgreen]$row[/color][COLOR=darkred]=[/color][b][COLOR=black]mysql_fetch_assoc[/color][/b][COLOR=darkred]([/color][COLOR=darkgreen]$res[/color][COLOR=darkred]))[/color] [COLOR=red]{[/color]
    [COLOR=darkgreen]$oldname[/color][COLOR=darkred]=[/color][COLOR=red]'tempdir/'[/color][COLOR=darkred].[/color][b][COLOR=black]basename[/color][/b][COLOR=darkred]([/color][COLOR=darkgreen]$row[/color][COLOR=darkred][[/color][COLOR=red]'imagename'[/color][COLOR=darkred]]);[/color] [i][COLOR=maroon]// or set $oldname as needed...[/color][/i]
    [COLOR=darkgreen]$newname[/color][COLOR=darkred]=[/color][COLOR=red]'imgdata/hotel/'[/color][COLOR=darkred].[/color][b][COLOR=black]basename[/color][/b][COLOR=darkred]([/color][COLOR=darkgreen]$row[/color][COLOR=darkred][[/color][COLOR=red]'imagename'[/color][COLOR=darkred]]);[/color] [i][COLOR=maroon]// or set $newname as needed...[/color][/i]
    [b][COLOR=black]rename[/color][/b][COLOR=darkred]([/color][COLOR=darkgreen]$oldname[/color][COLOR=darkred],[/color][COLOR=darkgreen]$newname[/color][COLOR=darkred]);[/color]
  [COLOR=red]}[/color]
[COLOR=red]}[/color]

Feherke.
 
Wow that was quick, thank you very much.

I will get cracking with this now, and let you know.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top