SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    ********* Streaker Lister14's Avatar
    Join Date
    Jun 2002
    Location
    C:\Documents and Settings\Desktop\Recycle Bin
    Posts
    677
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Probelm displaying files from directory

    Hello folks,

    Please view the following page: http://www.roy.lu/futuraplay/

    I am using this code:

    PHP Code:
    $dir="./download"; // 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);
    }
    1. Why does this display a . and a .. in addition to the files in the directory? - I don't want this
    2. Why don't the links include the real file path? Hence the pictures won't open...

    Thanks in advance for your help.

    Roy
    roy.lu
    C:\DOS... C:\DOS\RUN... RUN\DOS\RUN...!

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lister14 View Post
    1. Why does this display a . and a .. in addition to the files in the directory? - I don't want this
    Because you didn't ask it to differentiate between files and directories. Each folder in linux contains a . and .. for current folder and parent folder. You can use is_file($dir . '/' . $filename) to get rid of them.

    Quote Originally Posted by Lister14 View Post
    2. Why don't the links include the real file path? Hence the pictures won't open...
    Because $filename is just the file name. You need to use $dir . '/' . $filename to get the full path.

  3. #3
    ********* Streaker Lister14's Avatar
    Join Date
    Jun 2002
    Location
    C:\Documents and Settings\Desktop\Recycle Bin
    Posts
    677
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I understand what you are saying, but I can't figure out where/ how i need to change my code.
    A little help please?
    roy.lu
    C:\DOS... C:\DOS\RUN... RUN\DOS\RUN...!

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Certainly,
    PHP Code:
    <?php
    $dir
    ="./download"// Directory where files are stored 

    if ($dir_list opendir($dir)) 

       while((
    $filename readdir($dir_list)) !== false
       {
          
    $pathToFile $dir '/' $filename;
          if (
    is_file($pathToFile)) // added this and changed the link to use $pathToFile
          

    ?> 
       <p><a href="<?php echo $pathToFile?>"><?php echo $filename?></a></p> 
    <?php 
          
    }
       } 
       
    closedir($dir_list); 
    }
    ?>

  5. #5
    ********* Streaker Lister14's Avatar
    Join Date
    Jun 2002
    Location
    C:\Documents and Settings\Desktop\Recycle Bin
    Posts
    677
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot, I did manage to get the link working properly, but I'm not familiar enough with PHP yet to do this kind of magic...
    I was trying something along the lines of:

    if (($dir_list = opendir($dir)) && is_file($dir . '/' . $filename))
    { ...

    but this wasn't as logical to PHP as it seemed to me
    roy.lu
    C:\DOS... C:\DOS\RUN... RUN\DOS\RUN...!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •