#1
  1. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    Repost due to everyone ignoring my previous post! :P

    What I need to be able to do is to list all the HTML or PHP3 or TXT files in a variable-defined directory, with a little icon next to each one and each one needs to be able to carry a link. Anyone got any ideas as to how to do that? The link that needs to be carried will contain the name of the file.

    ------------------------------------------
    Alex
    (http://www.alex-greg.co.uk)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    15
    I think this will work for you

    <?php
    $workingdir = "/www/somedir";
    chdir($workingdir);
    $dir = dir(".");

    $dir->rewind();
    while ($file=$dir->read()) {
    if (($file != ".") &#0124; &#0124; ($file != "..")) {
    $filelist[] = $file;
    }
    }
    $dir->close();

    asort($filelist);

    while (list ($key, $value) = each ($filelist)) {
    if (eregi("php3", $value)) {
    $icon = '<img src="www.somewhere.com/images/phpicon.gif">'. "n";
    $newerfilelist[] = $value;
    }
    elseif (eregi("html", $value)) {
    $icon = '<img src="www.somewhere.com/images/htmlicon.gif">'. "n";
    $newerfilelist[] = $value;
    }
    elseif (eregi("txt", $value)) {
    $icon = '<img src="www.somewhere.com/images/txticon.gif">'. "n";
    $newerfilelist[] = $value;
    }
    else {
    $icon = "";
    }
    }
    while (list ($key, $val) = each ($newerfilelist)) {
    printf('<a href="http://www.nowhere.com/%s">%s</a><br>%s', $val, $val, "n");
    }

    ?>
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    15
    Forgot about printing the icon, just change this piece out with the one I supplied before:
    while (list ($key, $val) = each ($newerfilelist)) {
    printf('%s<a href="http://www.nowhere.com/%s">%s</a><br>%s', $icon, $val, $val, "n");
    }
  6. #4
  7. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    >>if (eregi("php3", $value)) {
    "php3.some_extension" would be treated as php file


    [This message has been edited by freebsd (edited July 02, 2000).]
  8. #5
  9. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    Thanks very much for the code. It works, apart from the icon bit:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>

    while (list ($key, $value) = each ($filelist)) {
    if (eregi("php3", $value)) {
    $icon = '<img src="www.somewhere.com/images/phpicon.gif">'. "n";
    $newerfilelist[] = $value;
    }
    elseif (eregi("html", $value)) {
    $icon = '<img src="www.somewhere.com/images/htmlicon.gif">'. "n";
    $newerfilelist[] = $value;
    }
    elseif (eregi("txt", $value)) {
    $icon = '<img src="www.somewhere.com/images/txticon.gif">'. "n";
    $newerfilelist[] = $value;
    }
    else {
    $icon = "";
    }
    }

    [/code]

    Each time, the else.... condition is met - I tested this by typing something in the $icon value for it, and this came out next to each file. I've had a look at it and I can't see where the problem lies.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    300
    Rep Power
    15
    1) The regular expressions above should have a dot before the extension, e.g. eregi(".html",$value). If not, then something like html.inc would be treated as an html file. I guess that is not what you would want to happen.

    2) Why not just use a system() call with a shell command?? Something like "ls *.html"??

    [This message has been edited by dwarf (edited July 02, 2000).]
  12. #7
  13. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    For some reason, it's now the PHP file condition that's always met. Here is my modified code:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>

    <?

    $workingdir = "";
    chdir($workingdir);
    $dir = dir(".");

    $dir->rewind();
    while ($file=$dir->read()) {
    if (($file != ".") &#0124; &#0124; ($file != "..")) {
    $filelist[] = $file;
    }
    }
    $dir->close();

    asort($filelist);

    include ("top.txt");
    print 'Choose the file to edit:</font><br><br>';
    print "<table border="0">";

    while (list ($key, $value) = each ($filelist)) {
    if (eregi(".php3",$value)) {
    $icon = '<tr><td><img src="php_icon.gif"></td> '. "n";
    $newerfilelist[] = $value;
    }
    elseif (eregi(".html",$value)) {
    $icon = '<tr><td><img src="html_icon.gif"></td> '. "n";
    $newerfilelist[] = $value;
    }
    elseif (eregi(".txt",$value)) {
    $icon = '<tr><td><img src="txt_icon.gif"></td> '. "n";
    $newerfilelist[] = $value;
    }
    else {
    $icon = '<tr><td><img src="misc_icon.gif"></td> ';
    }
    }
    while (list ($key, $val) = each ($newerfilelist)) {
    printf('%s<td><a href="edit_file.php3?file=%s"><font face="Verdana, Arial, Helvetica, san-serif" size=1>%s</a></td></tr>%s', $icon, $val, $val, "n");
    }

    print "</table>";
    include ("bottom.txt");

    ?>

    [/code]

    Please, someone, help me out! What was that system() command and how does that work? ....
  14. #8
  15. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    >>e.g. eregi(".html",$value). If not, then something like
    >>html.inc would be treated as an html file
    Exactly. To be more precise (I am not sure if you could do this in PHP), add a dollar sign like: eregi(".html$",$value);
    which will not treat "foo.html.php3" as html file.

    >>What was that system() command and how does that work?
    I don't know the PHP way but it could be easily done with Perl. Maybe someone else like Shiju can help you out with this.

    [This message has been edited by freebsd (edited July 02, 2000).]
  16. #9
  17. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    OK, I'm working with PHP here, not Perl! Uhm, the problem I'm having is with the icons: I want a different icon displayed for each file type. The PHP file type is the top in the list and this is the only icon that ever gets displayed. Here's the code again for that part:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>

    while (list ($key, $value) = each ($filelist)) {
    if (eregi(".php3$",$value)) {
    $icon = '<tr><td><img src="php_icon.gif"></td> '. "n";
    $newerfilelist[] = $value;
    }
    elseif (eregi(".html$",$value)) {
    $icon = '<tr><td><img src="html_icon.gif"></td> '. "n";
    $newerfilelist[] = $value;
    }
    elseif (eregi(".txt$",$value)) {
    $icon = '<tr><td><img src="txt_icon.gif"></td> '. "n";
    $newerfilelist[] = $value;
    }
    else {
    $icon = '<tr><td><img src="misc_icon.gif"></td> ';
    }
    }

    [/code]

    Please help! Forget the system() thing for now! Let's concentrate on this. Thanks!
  18. #10
  19. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    "Freddy Does PHP" wrote the code, I am sure he is willing to debug his own code ASAP. So have you mailto:freddy@bereminded.com?
    I am available, but sorry, I can't help you with this.
    Good Luck!

Similar Threads

  1. Replies: 3
    Last Post: February 17th, 2004, 10:08 AM
  2. How to write a PHP script to remove a file?
    By xmxpcom in forum PHP Development
    Replies: 2
    Last Post: February 16th, 2004, 05:58 PM
  3. PHP file upload script trouble
    By casbboy in forum PHP Development
    Replies: 9
    Last Post: February 5th, 2004, 07:42 PM
  4. Simple find and replace word search for a text file with php?
    By rabid lemming in forum PHP Development
    Replies: 4
    Last Post: February 1st, 2004, 09:37 PM
  5. Need help generating pdf or jpeg file with php.
    By network89 in forum PHP Development
    Replies: 0
    Last Post: February 1st, 2004, 09:13 PM

IMN logo majestic logo threadwatch logo seochat tools logo