Jump to content

PHP help

Featured Replies

Ok, I have looked around and can't really find a good, easy way to do this. I have about 100 or so files on my web server that I would like to link to, and rather then going in and putting

 

<a href="/fictionall/file1.txt">File1.txt</a>

<a href="/fictionall/file2.txt">File2.txt</a>

<a href="/fictionall/file3.txt">File3.txt</a>

 

Is there a PHP script that can read the files in a given directory and put the <a href=""></a> stuff in with regex?

if there names like this "file1.txt,file2.txt,file3.txt...." and so on,you can use this simple code

 

<?
for($s=0;$s<100;$s++)
{ ?>
<a href="/fictionall/file<?print $s; ?>.txt">File<?echo $s; ?>.txt</a>
<? } ?>

If they arent simply File1.txt, File2.txt but are more complicated file names you can use -

 

<?php
$dp = opendir('/fictionall/');
while ( $file = readdir($dp) ) {
   if ( !in_array( $file, array('.','..') ) ) {
       echo '<a href="/fictionall/' . $file . '">' . $file . "</a>\n";
   }
}
closedir($dp);
?>

 

That will allow just a simply flat directory with any file names. You can obviously just add particular file names to the array() in the if statement to exclude them from display. If there are alot of files you dont want to display, then using this method might not be the best and instead you might want to use something along the lines of glob() if your files match a particular pattern such as a particular extension or something, or you could add more checks such as various regexes or other things when checking if a file shouldnt be displayed.

 

If you have a simply directory structure that needs to be displayed rather than a flat directory, this would be more helpful -

<?php
function showDir( $dir )
{
   $dp = opendir($dir);
   while ( $file = readdir($dp) ) {
       if ( !in_array( $file, array('.','..') ) ) {
           if ( is_dir($dir . '/' . $file) ) {
               showDir($dir . '/' . $file);
           } else {
               echo '<a href="/' . $dir . '/' . $file . '">/' . 
                       $dir . '/' . $file . "</a>\n";
           }
       }
   }
   closedir($dp);
}
showDir('fictionall');
?>

 

If you have lots and lots and lots and lots of directories beneath the directory you're listing, be careful as this function is recursion and you dont want to keep shoving function callbacks onto the stack. Again you can alter it to exclude certain files and can include other checks such as regexes.

 

It is fairly simple to adds to this recursive function to allow it to perhaps display the directory name, then all of the files under it and so on (perhaps passing in a number of character indents to indent each subdir across from the main dir or something) etc.

 

Again this is only if the files arent simply named, and if you want to simply be able to chuck a file in the directory and not worry about renaming it with a number or wish to rename it to something more meaningful to the user.

Archived

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

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.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.