list files from directory on a site

C

chlori

Hello

I would like to have a list of links on a website linking to
the files in a given directory (each file has one link).
This list should get updated when the files from that dir
are edited (files added, removed, renamed, ...) with a
separate FTP-client. Is that possible?
 
M

Martin Jay

chlori <[email protected]> said:
I would like to have a list of links on a website linking to the files in
a given directory (each file has one link). This list should get updated
when the files from that dir are edited (files added, removed, renamed,
...) with a separate FTP-client. Is that possible?

Yes, and quite easy to do. Some web servers will display a list of all
files in a directory if that directory doesn't contain an 'index' file.

If you can run PHP files on your server you could use something like
this:

<?php

$dir=""; // Directory where files are stored

if ($dir_list = opendir($dir))
{
while(($filename = readdir($dir_list)) !== false)
{
?>
<p><a href="<?php echo $filename; ?>"><?php echo $filename;
?></a></p>
<?php
}
closedir($dir_list);
}

?>
 
J

Jim Moe

chlori said:
I would like to have a list of links on a website linking to
the files in a given directory (each file has one link).
This list should get updated when the files from that dir
are edited (files added, removed, renamed, ...) with a
separate FTP-client. Is that possible?
Yes.
The simple way is a server-side processor like PHP. It reads the
directory obtaining the list of files and generates a list of links to
those files. This happens every time the page is loaded.
 
C

chlori

Martin said:
If you can run PHP files on your server you could use something like
this:

<?php

$dir=""; // Directory where files are stored

if ($dir_list = opendir($dir))
{
while(($filename = readdir($dir_list)) !== false)
{
?>
<p><a href="<?php echo $filename; ?>"><?php echo $filename;
?></a></p>
<?php
}
closedir($dir_list);
}

?>

I get an "Internal Server Error". Any ideas?

Is there some server config stopping it from working or am I doing
something wrong?
 
M

Martin Jay

[QUOTE="chlori said:
If you can run PHP files on your server you could use something like
this:
<?php
$dir=""; // Directory where files are stored
if ($dir_list = opendir($dir))
{
while(($filename = readdir($dir_list)) !== false)
{
?>
<p><a href="<?php echo $filename; ?>"><?php echo $filename;
?></a></p>
<?php
}
closedir($dir_list);
}
?>

I get an "Internal Server Error". Any ideas?

Is there some server config stopping it from working or am I doing
something wrong?[/QUOTE]

Hmmm, I haven't seen that non-specific error since my Perl days. :)

There may be some more useful information in your log.

Perhaps '$dir' hasn't been set correctly. For example: my domain name
is spam-free.org.uk, but my host stores my files at:

/homepages/0/[REMOVED]/htdocs/spam-free.org.uk/

I've upload the full .php file to:

http://www.spam-free.org.uk/pages/dir.zip

And this is the sort of output you should get form it:

<http://www.spam-free.org.uk/pages/dir/>
 
J

Jim Moe

chlori said:
I get an "Internal Server Error". Any ideas?
Also:
- Is PHP enabled on your server?
- Did you change the name of the file to have a .php extension?
 
M

Martin Jay

Jim Moe said:
That should be "readdir($dir_list)) != false".

It's better to use !== in case there is a file named '0' (as I wasted
quite some time and effort discovering a while ago). :(
 
S

Steven Saunderson

Ah! One of those bizarre extras.

Yes, it is a bit odd but it is a very necessary and useful feature.
I apologise for my superfluous reply but Martin's more informative reply
hadn't appeared at news.individual when I posted mine.

I don't know why "server internal error" is being reported. If I have
PHP syntax errors I generally get no response but do get a meaningful
message in the error log.
 
M

Mark Parnell

Deciding to do something for the good of humanity, chlori
I would like to have a list of links on a website linking to
the files in a given directory (each file has one link).
This list should get updated when the files from that dir
are edited (files added, removed, renamed, ...) with a
separate FTP-client. Is that possible?

Assuming it is an Apache server, just create a .htaccess file and put it
in that directory, with the following in the file:

Options +Indexes
 
C

chlori

Jim said:
What is in your error_log for the error?

[Fri May 5 10:07:04 2006] [error] [client xx.xx.xxx.xx] Premature end
of script headers: /home/xyxyx/public_html/xxxx/links.php
 
C

chlori

Jim said:
- Is PHP enabled on your server?
Yes!

- Did you change the name of the file to have a .php extension?

Yes, I did. I am also using php includes and they work....
 
C

chlori

Martin said:
Perhaps '$dir' hasn't been set correctly. For example: my domain name
is spam-free.org.uk, but my host stores my files at:

/homepages/0/[REMOVED]/htdocs/spam-free.org.uk/

First I had it wrong, but then I tried this way and it still didn't work...
 
S

Steven Saunderson

Jim said:
What is in your error_log for the error?

[Fri May 5 10:07:04 2006] [error] [client xx.xx.xxx.xx] Premature end
of script headers: /home/xyxyx/public_html/xxxx/links.php

The Apache 2 all_in_one.htm says : "Premature end of script headers"

"Most problems with CGI scripts result in this message written in the
error log together with an Internal Server Error delivered to the
browser. A guide to helping debug this type of problem is available in
the CGI tutorial."

Perhaps the CGI engine is trying to process your PHP file.
 
C

chlori

Hmmm, I haven't seen that non-specific error since my Perl days. :)

The problem is solved:
When I changed the permissions for the script from 666 to 644 it works.
Don't know why, but it works...

My next questions:
- If I have files and directories it lists both the same way. I would
like to add a class="dir" to the directory links so I can change the
look with CSS.
- The List shows ".", ".." and ".htaccess". How can I hide these?

BTW, I'm using this script:

<?php

$dir="/home/xyxy/public_html/xyxyx/files";

if ($dir_list = opendir($dir))
{
while(($filename = readdir($dir_list)) !== false)
{
?>
<li><a href="files/<?php echo $filename; ?>"><?php echo $filename;
?></a></li>
<?php
}
closedir($dir_list);
}

?>
 
M

Martin Jay

chlori <[email protected]> said:
My next questions:
- If I have files and directories it lists both the same way. I would
like to add a class="dir" to the directory links so I can change the
look with CSS.
- The List shows ".", ".." and ".htaccess". How can I hide these?

<style type="text/css">
<!--
.file{
padding-left: 1em;
}
.dir{
padding-left: 0;
}
-->
</style>

<?php

$dir="";

if ($dir_list = opendir($dir))
{
while(($filename = readdir($dir_list)) !== false)
{
if($filename != "." && $filename != ".." && $filename
!= ".htaccess") // check for '.' '..' '.htaccess'
{
if(is_dir($dir.$filename))
$class="dir";
else
$class="file";
?>
<li class="<?php echo $class; ?>"><a
href="files/<?php echo $filename; ?>"><?php echo $filename;?></a></li>
<?php
}
}
closedir($dir_list);
}

?>


I was thinking about sorting the directory list alphabetically.
Surprisingly, there is no easy way to load a directory listing into an
array in PHP. Very disappointing. :(

As for this script: handling directories would need more fiddling.
Perhaps using a link such as ?dir=$dir.$filename for directories, then
making $dir=$_GET['dir'].

If you just want a simple clickable list of files it will probably be
easier to get your server to provide one. :(

The script I posted was a cut-down version of something else I was
working on recently to display pictures from a directory.
 
M

Martin Jay

[QUOTE="chlori said:
if(is_dir($dir.$filename))
$class="dir";
else
$class="file";
[/QUOTE]
Now all links get a class="file", even if I have dirs and files... ?

Ooops. I forgot to mention that $dir needs a trailing /.

So:

$dir="/home/xyxy/public_html/xyxyx/files";

becomes:

$dir="/home/xyxy/public_html/xyxyx/files/";

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top