While loop for unlink from a text file?

If I have a text file (called test.txt) which contains three lines:

page.one.php
page-two.php
page-three.php

How can I make a while loop which opens test.txt and deletes the three pages contained within?

Like:


<?php
$file = "test.txt";
while () {
This is the part I don't know. I want to look for in test.txt and delete the three pages that are listed there.
}
?>

…I don’t know how to do it. I have been trying for ages.

The file I have is a test file called monsters.php

inside monsters.php is two lines:

big-monsters.php
small-monsters.php

I want to open mosters.php

Then I want to delete:

$_SERVER[‘DOCUMENT_ROOT’] . ‘/big-monsters.php’;
and
$_SERVER[‘DOCUMENT_ROOT’] . ‘/small-monsters.php’;

If I echo the line (as I have above) all it says is monsters.php

I don’t want to put unlink anywhere near that flename. That is not to be deleted.

file()

Hmm, do you still have this at the top of your script?


<?php 
session_start(); 
$edit_page = $_SESSION['edit_page'];
$delete_category = $_POST['delete_category'];
echo $delete_category;
[B][I][COLOR="Red"]exit;[/COLOR][/I][/B]
?>


Get rid of that exit :slight_smile:

This is my finished file :smiley:


<?php 
session_start(); 
$edit_page = $_SESSION['edit_page'];
$delete_category = $_POST['delete_category'];
?>
<?php
$file = $_SERVER['DOCUMENT_ROOT'] . '/category-associations/'.$delete_category.'';
 
if ($lines = file($file)) {
  // file() has been successful
  foreach($lines as $line) {
        unlink($_SERVER['DOCUMENT_ROOT'] . '/'.trim($line).'');
        //echo $line;
  }
    foreach($lines as $line) {
        unlink($_SERVER['DOCUMENT_ROOT'] . '/pages/'.trim($line).'');
        //echo $line;
  }
} else  {
  // file() has not been successful
  echo "file($file) has not been successful!<br/>";
} 
$delete_page1 = $_SERVER['DOCUMENT_ROOT'] . '/category-associations/'.$delete_category.'';
unlink($delete_page1);
$delete_page2 = $_SERVER['DOCUMENT_ROOT'] . '/category-menus/'.$delete_category.'';
unlink($delete_page2);
$_SESSION['edit_page'] = "./";
//==Delete the link for the category from the topmenu
$delete_category = $_POST['delete_category'];

$delete_name = $_POST['delete_category'];
$delete_name = ereg_replace('.php','',$delete_name);
$delete_name = ereg_replace('-',' ',$delete_name);
$delete_name = ucwords($delete_name);

$delete_class = $_POST['delete_category'];
$delete_class = ereg_replace('-','_',$delete_class);
$delete_class = ereg_replace('.php','',$delete_class);
$delete_class = strtolower($delete_class);
$file = $_SERVER['DOCUMENT_ROOT'] . '/inc/topmenu-include.php';
$line = '<div class="'.$delete_class.'"><a href="/'.$delete_category.'">'.$delete_name.'</a></div>';
function delete_category1($find, $file)
{
    $contents = array();
    $new_contents = "";
    $find = trim($find);
    $return = false;
 
    if(file_exists($file))
    {
        if(is_writable($file))
        {
            // open the file as an array, each line as an element
            if($contents = file($file))
            {
                    // remove extra whitespace from each line
                    $contents = array_map("trim", $contents);
                    
                    // loop through each line and see if it's the one we want<br />
                    foreach($contents as $line_number => $line)
                    {
                        $lowercase_line = strtolower($line);
                        $lowercase_find = strtolower($find);
                        if($lowercase_line == $lowercase_find)
                        {
                            // this is where we remove the line
                            unset($contents[$line_number]);
                            $return = true;
                        }
                    }
                    
                    // put the lines back together with a \
 between each
                    $new_contents = implode("\
", $contents);
                    
                    // open the file and delete its old contents
                    if(!$handle = fopen($file, 'w'))
                    {
                        trigger_error("$file could not be opened for writing", E_USER_NOTICE);
                        return false;
                    }
                    else
                    {
 
                        if(fwrite($handle, "".$new_contents."\
") !== false)
                        {
                            // it worked
                            return $return;
                        }
                    }
            }
            else
            {
                trigger_error("$file is not readable", E_USER_NOTICE);
                return false;
            }
        }
        else
        {
            trigger_error("$file is not writable", E_USER_NOTICE);
            return false;
        }
    }
    else
    {
        trigger_error("$file does not exist", E_USER_NOTICE);
        return false;
    }
    
}


if(delete_category1($line, $file))

header('Location: /admin/delete-category.php');
?>

See, this is what I mean. I didn’t even know that.

Without help, I would have been here for 3 months and I wouldn’t have a functioning computer anymore.

Thanks for the help, I appreciate it.

I tried to do it myself for hours, but I couldn’t - no matter what I tried. I know the principal of what I need to do but I am not familiar enough with the different codes I can use to know how to make it or even what to look up. I tried…

An enter in a text file is actually coded as \

the file() function returns the file as an array, but doesn’t remove the
at end of the line, so that’s actually passed as a parameter to the unlink function.
[fphp]trim/fphp removes that
at the end :slight_smile:

What clue did you give me that this would be the case you ask?


Warning: unlink(some/path/big-monsters.php ) [function.unlink]: Invalid argument
                                          [COLOR="Red"][B]^[/B] that space right there[/COLOR]

Always pay attention to the small details when debugging :slight_smile:

I am lost…

use trim($line) instead $line in the unlink
and please try to find a little more patience instead of bumping this thread every few minutes …

That fixed it :slight_smile:

What did that do?

I am happy now…

…man, I was about to snap my keyboard in half. I have been trying to make this work for 1/2 a day! I was very frustrated.

It is for a script I’ve been writing all week and this is one of the last parts I am doing becaise it is hard for me. But now I can finishe the script. The re st of the PHP in the script is stuff I understand.

I am writing a little CMS. If anyone wants a look lemme know. It’s practically finished.

I will likeyour opinion.

Thanks guido2004 and ScallioXTX :slight_smile:

…and this is line 14:

unlink($_SERVER[‘DOCUMENT_ROOT’] . ‘/’.$line.‘’);

…and yes, the files exist.

I changed it to unlink and this is what it said:

monsters.phpfile: C:/Documents and Settings/Administrator/Desktop/Websites/UFO/www/category-associations/monsters.php

Warning: unlink(C:/Documents and Settings/Administrator/Desktop/Websites/UFO/www/big-monsters.php ) [function.unlink]: Invalid argument in C:\Documents and Settings\Administrator\Desktop\Websites\UFO\www\admin\delete-category-form.php on line 14
big-monsters.php
Warning: unlink(C:/Documents and Settings/Administrator/Desktop/Websites/UFO/www/small-monsters.php ) [function.unlink]: Invalid argument in C:\Documents and Settings\Administrator\Desktop\Websites\UFO\www\admin\delete-category-form.php on line 14
small-monsters.php

yes

I am scare to do it in case it starts deleting stuff it’s not supposed to.

Of course you’ll test this in a test environment first. And make a backup before you start deleting anything :lol:

It says this:

monsters.phpfile: C:/Documents and Settings/Administrator/Desktop/Websites/UFO/www/category-associations/monsters.php
big-monsters.php small-monsters.php

See, it works :smiley:
My link was enough, you found the solution all by yourself. Too bad that exit messed things up :wink:

Ok, so I just ahve to change the echos to unlinks?

I am scare to do it in case it starts deleting stuff it’s not supposed to.

I’ll have a hover car before I get this script working!

It just returns a page that says:

monsters.php

If your echo is showing the path to the file, uncomment the unlink. If it is not showing the path, you will need to tweak something but you are close to having a working script.