Click to See Complete Forum and Search --> : listing a directory & its contents as html


hammerslane
10-14-2003, 09:26 AM
i was wondering if i could do a while loop or something which would display the contents of an online directory (excel documents).. the contents would change regularly so it has to update dynamically/automatically. every different file would be displayed on a different line, and would link to that file (it would actually link to another php file, containing an iframe with the excel document in it).
it's kind of like displaying all of the records in a table in a database, except just the contents of a folder.
i'm pretty sure it's possible... i'm just not good enough at php to know how to do it!
any help/links/comments/actions are appreciated.
thanks people :cool:

pyro
10-14-2003, 09:42 AM
Try something like this:

<?PHP
$dir = "/root/path/to/currentdirectory/";
$handle = opendir($dir);
while ($file = readdir($handle)) {
if ($file != "." && $file != "..") {
echo "<a href=\"$file\">$file</a><br>";
}
}
closedir($handle);
?>

hammerslane
10-14-2003, 09:48 AM
Parse error: parse error, unexpected '.' in http://server01/invoice\invoice_xls\dirlist.php on line 5

4 while ($file = readdir($handle)) {
5 if ($file != "." && $file != "..") {
6 echo "<a href=\"$file\">$file</a><br>";

my php knowledge is rather primitive, i can make out what code does, but i can't tweak it too much, and i definitely can't make complex php soley from php.

do i have to do something with $file ?

pyro
10-14-2003, 09:59 AM
It should have worked, if you path was set correctly. However, I should have done it like this, so use this one:

<?PHP
$dir = "temp";
$handle = opendir($dir);
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "<a href=\"$file\">$file</a><br>";
}
}
closedir($handle);
?>

hammerslane
10-14-2003, 10:28 AM
thanks very much pyro - the latter suggestion worked, i had to do the actual folder name (without the http://localhost etc bit at the start).

thanks again!!

[RESOLVED]

pyro
10-14-2003, 10:37 AM
You are welcome... :)

hammerslane
10-15-2003, 10:36 AM
hey hey! me again - if you're reading this post and you are, for some reason, in the frame of mind to help me, this question stems off my first question which is the initial post in this thread.

once you've read that lot, here is my next question, and all answers will help me, no matter how little you know :)

i can now display all the files in my directory (see the small attached picture).
i'm now trying to get those little edit/ delete/ add invoice buttons to work... anyone have any idea or a function which will open a little pop up where you can type a file name (in the edit/add case) and will just delete the file in the delete case?

would javascript be better? any little snippets of code, any useful pages, and general input about what langauge would be better (etc etc) would be much appreciated by everyone/anyone.

thanks people (ie pyro :p )
regards

pyro
10-15-2003, 11:34 AM
How I would do it is just have the file get deleted when the click the button (perhaps after a confirmation, which can be done easily in JavaScript). To delete a file, you are going to want to use unlink (http://us3.php.net/unlink).

hammerslane
10-16-2003, 04:19 AM
thanks again pyro :)

<?
unlink ("$dir/$file");
>?
(^ the working code)

i spent about an hour on php.net to find that copy() and rename() were the best ways to try to create and rename.

if anyone has anything to say that might be of use to me, i'll read it thankfully.

pyro
10-16-2003, 07:25 AM
One thing...

unlink() (http://us3.php.net/unlink) only works for files. If you want to remove directories, you'll have to use rmdir() (http://us3.php.net/manual/en/function.rmdir.php). Also, with rmdir, the directories must be empty, so you'll either have to write a recursive function, or, more dangerously, you could run a shell command ( shell_exec() (http://us3.php.net/manual/en/function.shell-exec.php) or the backtick operator (http://us3.php.net/manual/en/language.operators.execution.php) ) such as

<?PHP
`rm -rf /path/to/directory`;
?>Be very careful with the above, however, as you point it to the wrong directory, you could end up losing all your files.

hammerslane
10-16-2003, 10:11 AM
ah right, i saw something like that on php.net - it seemed quite dangerous... i think if i need a directory deleting, i'll do it through windows explorer. :)

my solution (for reference of future people who might visit this thread) was this:
rename("$dir/$folder/$file", "$dir/$folder/$newfile");all the variables had to be passed through the form in hidden text boxes. the $newfile was defined on the previous page, made soley for entering a new file name.

for my last trick... i will get the code (see above attachment) to copy ACTIVE.XLS to a new file called UNTITLED.XLS when the user clicks 'Add an Invoice'. they'll then have to go through the RENAME process to name it what they want.

wish me luck. ;)


note:
if anyone knows about IIS, then i have a question about it. to save space, i'll just link to the question which is here (http://www.iisfaq.com/forums/topic.asp?TOPIC_ID=1913&FORUM_ID=3&CAT_ID=5&Topic_Title=Mac+IIS+compatibility+issue&Forum_Title=General+Discussions). it's about getting http://localhost/intranet to show up on macintosh computers, rather than the mac browser looking on the internet.