Results 1 to 9 of 9

Thread: how to easily delete a file through php

  1. #1
    Join Date
    Apr 2007
    Posts
    149
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to easily delete a file through php

    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.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Apr 2007
    Posts
    149
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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...

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

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    echo file_get_contents($file_namepath); is a lot easier.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Apr 2007
    Posts
    149
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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...

  6. #6
    Join Date
    May 2008
    Posts
    17
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default

    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 .
    Last edited by shotgun_ninja; 05-21-2008 at 04:15 PM.

  7. #7
    Join Date
    May 2008
    Posts
    17
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default

    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:

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

  8. #8
    Join Date
    Apr 2007
    Posts
    149
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Ok, so far i've got this to display the files:

    PHP Code:
    <?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?
    Last edited by remp; 05-21-2008 at 10:46 PM.

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You'll need to start combining the ideas in this thread. Get the names, go through a loop, and use file_get_contents().
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •