PDA

View Full Version : how to easily delete a file through php



remp
05-19-2008, 11:22 PM
Ok, here is my situation... I have a coment scripts that will create a .txt file per message in a directory called "messages", and i want to create sort of a admin panel with the only function of deleting this files... i want display the file and the content so that the administrator can see it and if is not appropiate, delete the file....what will be the easiest way of doing this? I have basic knowledge of php and mysql... any help will be appreciated a bunch!!! And thanks in advanced.

djr33
05-19-2008, 11:44 PM
This function will easily kill just about anything on your server, and easily.
http://www.php.net/manual/en/function.rmdir.php#83126

There is no undo, so use with caution!

Of course unlink() and rmdir() have their specific purposes if you're not trying to do this in a recursive loop.

remp
05-20-2008, 04:26 AM
thanks, that sure was helpful, but in order to delete the files, i have to display them first...
i want to use something like this to display the content of the file but wut im not sure how to do is make it in a way that it will also display new files that are constanly coming in... these new files a ramdomly named...(ex. 24235 next file: 5345 it can be any number...) and if i use this code, it will only display the file specified...


<?php
$Handle = fopen('data.txt', 'r'); // Open the file for reading
fclose($Handle); // Close the file
?>

djr33
05-20-2008, 04:31 AM
echo file_get_contents($file_namepath); is a lot easier.

remp
05-20-2008, 05:51 AM
how about if i put the path to a directory (folder), would it display all the files in this folder? all of them are .txt files... because that is my goal after all, to display the files, then set a link to remove them...

shotgun_ninja
05-21-2008, 04:07 PM
glob() function, gets an array of filenames matching a search pattern. You know how to use arrays, don't you?

Say you're searching the path "files/path/":

glob("files/path/*");

EDIT: Not sure about this, check the PHP tutorials on http://www.php.net .

shotgun_ninja
05-21-2008, 04:14 PM
So, you want basically a list of image links of files? I'm inferring from this that you are trying to make an admin panel page or something. First, use glob() to get the array, then for each element, do something like this:



echo("<a href=\"" . $filepath . ">" .
"<img style=\"border:0px;\" src=\"images/" . $fileext . ".png\"><br />" .
$filename . "</a>");

remp
05-21-2008, 05:45 PM
Ok, so far i've got this to display the files:


<?php
$path = "mypath/web/guestbook2/messages/";

$dir_handle = @opendir($path) or die("Unable to open $path");

while ($file = readdir($dir_handle)) {

if($file == "." || $file == ".." || $file == "index.php" )

continue;

continue;
echo "<a href=\"messages/$file/\">$file</a> <div> <a href="../index.php"> delete </a> </div><br>";

}

// Close
closedir($dir_handle);

?>

but how can i get it display the content instead of displaying the name of the file?

djr33
05-22-2008, 12:15 AM
You'll need to start combining the ideas in this thread. Get the names, go through a loop, and use file_get_contents().