Jump to content

Php Manage files in a directory


anthonyw17

Recommended Posts

ok so im working on a script that lists files with a checkbox next to them and you can select the files and hit a button that deletes them using the unlink function, but it doesnt seem to work for me here this what i done so far:

 

list_files.php

<?php
$URL = "http://website.com";
print "<form method='post' action='delete_files.php'>";
if ($handle = opendir('/home/staticextasy/public_html')) {
   // echo "Directory handle: $handle\n";
    echo "<h4>Files:</h4>\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
        echo "<p><input type='checkbox' name='filename' value='$file'> <a href='$URL/$file'>$file\n</a></p>";
    }

    /* This is the WRONG way to loop over the directory. */
    while ($file = readdir($handle)) {
        echo "<p><input type='checkbox' name='filename' value='$file'> <a href='$URL/$file'>$file\n</a></p>";
    }

    closedir($handle);
}
print "<input type='submit' value='Delete'>";
?> 

 

delete_files.php

<?php
$filename = $_POST['filename'];
$delete = "/home/staticextasy/public_html/$filename";

chmod($delete, 0777);

unlink($delete);

header ('Location: http://website.com/admin/list_files.php');
?>

 

any ideas?

Link to comment
https://forums.phpfreaks.com/topic/168343-php-manage-files-in-a-directory/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.