robyngae Posted October 10, 2008 Share Posted October 10, 2008 I need to be able to display a directory and allow the user to have an option to delete the file, so need a button for each file. Will someone explain how to do this please, there are two directories involved an image one and an upload one. The image one I guess I would just use a gallery with the extra buttons. Link to comment Share on other sites More sharing options...
Lulzim Posted October 10, 2008 Share Posted October 10, 2008 these are some of the functions you would need to do thatopendir readdir unlink Link to comment Share on other sites More sharing options...
justsomeguy Posted October 10, 2008 Share Posted October 10, 2008 scandir is useful also:http://www.php.net/manual/en/function.scandir.phpThat will list all of the files and folders in the target directory. You can use is_dir to check if it's a directory, and only print the files and not directories. For the delete button, you need the link to include the filename to delete. Here's an example to print files using readdir: $directory = '/path/to/files'; $handle = opendir($directory); while (false !== ($item = readdir($handle))) { if($item != '.' && $item != '..') { $path = $directory.'/'.$item; if(!is_dir($path)) { echo $item . ' <a href="delete.php?f=' . urlencode($item) . '">Delete</a><br>'; } } } closedir($handle); The delete.php file can check $_GET['f'] and use that as the filename to delete, unlink will delete a file. Link to comment Share on other sites More sharing options...
robyngae Posted October 11, 2008 Author Share Posted October 11, 2008 these are some of the functions you would need to do thatopendir readdir unlink Thank you I have used opendir and readdir on my main page where I list the files in the directory. I also have options for the users, one being to delete files, so this will need to link to a new page that lists the contents with a seperate delete button against each one.I don't know the name of the file being deleted so can't insert it. Would somehow have to be linked. Link to comment Share on other sites More sharing options...
robyngae Posted October 11, 2008 Author Share Posted October 11, 2008 scandir is useful also:http://www.php.net/manual/en/function.scandir.phpThat will list all of the files and folders in the target directory. You can use is_dir to check if it's a directory, and only print the files and not directories. For the delete button, you need the link to include the filename to delete. Here's an example to print files using readdir: $directory = '/path/to/files'; $handle = opendir($directory); while (false !== ($item = readdir($handle))) { if($item != '.' && $item != '..') { $path = $directory.'/'.$item; if(!is_dir($path)) { echo $item . ' <a href="delete.php?f=' . urlencode($item) . '">Delete</a><br>'; } } } closedir($handle); The delete.php file can check $_GET['f'] and use that as the filename to delete, unlink will delete a file. Thanks, copied that out and it certainly going the way I want, I added $i=1; above "while" thinking it would give me a numbered list but that didn't work. I would like the delete to be a button similar to an upload button but does that mean it goes into a form? I also need to know what code I put in the delete.php file. I am relatively new to this so learning as I go.Robyn Link to comment Share on other sites More sharing options...
Synook Posted October 11, 2008 Share Posted October 11, 2008 I also need to know what code I put in the delete.php file. <?phpunlink($_GET[$item]);echo "$item has been deleted";?> Thanks, copied that out and it certainly going the way I want, I added $i=1; above "while" thinking it would give me a numbered list but that didn't work $handle = opendir($directory); echo "<ol>"; while (false !== ($item = readdir($handle))) { if($item != '.' && $item != '..') { $path = $directory.'/'.$item; if(!is_dir($path)) { echo "<li>" . $item . ' <a href="delete.php?f=' . urlencode($item) . '">Delete</a></li>'; } } } echo "</ol>"; Link to comment Share on other sites More sharing options...
robyngae Posted October 12, 2008 Author Share Posted October 12, 2008 <?phpunlink($_GET[$item]);echo "$item has been deleted";?> Thanks for the help I set up a delete.php page with the above code but I am getting error message Warning: unlink() [function.unlink]: No such file or directory in /home/robyngae/public_html/ICAB5165A/delete.php on line 2has been deleted $handle = opendir($directory); echo "<ol>"; while (false !== ($item = readdir($handle))) { if($item != '.' && $item != '..') { $path = $directory.'/'.$item; if(!is_dir($path)) { echo "<li>" . $item . ' <a href="delete.php?f=' . urlencode($item) . '">Delete</a></li>'; } } } echo "</ol>"; This one worked out well thank you. Link to comment Share on other sites More sharing options...
Synook Posted October 12, 2008 Share Posted October 12, 2008 Umm... look at the item on the querystring. Does it include the directory? If not, concatenate it on the unlink argument, e.g. <?phpunlink("path/to/file/{$_GET[$item]}");echo "$item has been deleted";?> Link to comment Share on other sites More sharing options...
robyngae Posted October 13, 2008 Author Share Posted October 13, 2008 Umm... look at the item on the querystring. Does it include the directory? If not, concatenate it on the unlink argument, e.g.<?phpunlink("path/to/file/{$_GET[$item]}");echo "$item has been deleted";?> Thanks did that <?phpunlink("uploads/{$_GET[$item]}");echo "$item has been deleted";?> but now I get the error notice Warning: unlink(uploads/) [function.unlink]: Is a directory in /home/robyngae/public_html/ICAB5165A/delete.php on line 2has been deleted Am I missing something simple? Message doesn't echo the "$item" part of the message and nothing has been deleted.Robyn Link to comment Share on other sites More sharing options...
Synook Posted October 13, 2008 Share Posted October 13, 2008 Err... does the URL you go to when you view that page look like "delete.php?f=" or "delete.php?f=filetodelete"? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 13, 2008 Share Posted October 13, 2008 Yeah it's better that the file only accept a filename, you don't want people finding a delete page and typing in various paths to see what they can do to your server (it's also not a great idea to use something from $_GET directly in a function like unlink). You can just check if the file exists first. <?php$path = dirname(__FILE__) . '/uploads/';$f = isset($_GET['f']) ? $_GET['f'] : '';if (file_exists($path . $f) && !is_dir($path . $f)){ unlink($path . $f); echo "$f has been deleted";}else echo $path . $f . ' is not a file';?> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.