A logic/Syntax Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sosha
Forum Newbie
Posts: 5
Joined: Tue Mar 21, 2006 1:52 am

A logic/Syntax Question

Post by sosha »

Hi,
I have a text file that contains names of files in a directory on my webspace. I would like to do is compare the files to the filenames in the textfile and delete the ones that dont exist.

The problem comes in comparing the textfile with the names of the files in the directory so as to unlink the ones that do not exist.
Any suggestions?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

loop through your text file

check if file_exists() and unlink if it doesn't
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
sosha
Forum Newbie
Posts: 5
Joined: Tue Mar 21, 2006 1:52 am

Post by sosha »

I did think of this but Im wondering, wont the first loop delete all the files that are not the same to the first filename in the textfile?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

do you want to delete the files from the server or do you want to remove them from the text file?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

loop through your text file and store the filenames into an array
then loop through your directory and store those filenames into an array

use array_diff() to find the difference

then do a foreach on the resulting array and unlink
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
sosha
Forum Newbie
Posts: 5
Joined: Tue Mar 21, 2006 1:52 am

Post by sosha »

Hi
Sorry for not being clear. I want to delete the files from the server using the textfile as the guide on which filesnames of files NOT to delete are saved
sosha
Forum Newbie
Posts: 5
Joined: Tue Mar 21, 2006 1:52 am

Post by sosha »

HEY!
It works..WOW!
Thanks a million, I owe u one!
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

ok heres a example:

the textfile

Code: Select all

file1.html
zoorg.html
beebop.htm
devnet.php
here is the directory listing on your server

Code: Select all

file1.html
zoorg.html
badfileahhh.php
beebop.htm
devnet.php
horribleness.html
and thus you want to delete 'badfileahhh.php' and 'horribleness.html' right? here is a quick example

Code: Select all

$txt = file_get_contents('textFile.txt');

$txtarr = explode("\n", $txt);

$dir = opendir('directory');

while (false !== ($file = readdir($dir)))
    $dirarr[] = $file;

$all = array_diff($dirarr, $txtarr);

if (is_array($all))
{
    foreach ($all as $key => $val)
        unlink('directory/'.$val);
}
of course you are going to want to test everything first before you start deleting files

edit: and once again I am too slow, by just seconds this time. maybe I am speeding up?
Post Reply