Results 1 to 5 of 5

Thread: Listing file in a directory problem

  1. #1
    Join Date
    Dec 2010
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation Listing file in a directory problem

    Im having some trouble with some code that lists the content of a directory. at the moment it is supose to list the name of the file, the type and the file size. the code is displaying the file name fine but not the File Type or File Size. im getting error "Warning: filetype() [function.filetype]: Lstat failed for Signiture.jpg in C:\xampp\htdocs\Login\dean\index.php on line 62"

    it works if i list the files in the same directory as this php code but not if i list files in a sub directory e.g $username/Uploads

    Here is the code im using
    PHP Code:
    <?php
    // open this directory 

    $myDirectory opendir("./Uploads/");

    // get each entry
    while($entryName readdir($myDirectory)) {
        
    $dirArray[] = $entryName;
    }

    // close directory
    closedir($myDirectory);

    //    count elements in array
    $indexCount    count($dirArray);
    Print (
    "$indexCount files<br>\n");

    // sort 'em
    sort($dirArray);

    // print 'em
    print("<TABLE  border=0 cellpadding=5 cellspacing=15 class=whitelinks>\n");
    print(
    "<TR><TH>File Name</TH><th>File Type</th><th>File Size</th></TR>\n");

    // loop through the array of files and print them all
    for($index=0$index $indexCount$index++) {
            if (
    substr("$dirArray[$index]"01) != "."){ // don't list hidden files
            
    print("<TR><TD><a href=\"./Uploads/$dirArray[$index]\">$dirArray[$index]</a></td>");
            print(
    "<td>");
            print(
    filetype($dirArray[$index]));
            print(
    "</td>");
            print(
    "<td>");
            print(
    filesize($dirArray[$index]));
            print(
    "</td>");
            print(
    "</TR>\n");
        }
    }
    print(
    "</TABLE>\n");

    ?>

  2. #2
    Join Date
    Dec 2010
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Anyone got any idea on what the problem may be ? i still have no luck

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    You need to include the location of the file in filetype and filesize.



    PHP Code:
    <?php
    // open this directory 
    $directory "./Uploads/";
    $myDirectory opendir($directory);

    // get each entry
    while($entryName readdir($myDirectory)) {
        
    $dirArray[] = $entryName;
    }

    // close directory
    closedir($myDirectory);

    //    count elements in array
    $indexCount    count($dirArray);
    Print (
    "$indexCount files<br>\n");

    // sort 'em
    sort($dirArray);

    // print 'em
    ?>
    <table  border="0" cellpadding="5" cellspacing="15" class="whitelinks">
        <tr>
            <th>File Name</th>
            <th>File Type</th>
            <th>File Size</th>
        </tr>
    <?php
    // loop through the array of files and print them all
    for($index=0$index $indexCount$index++) {
        if (
    substr("$dirArray[$index]"01) != "."){ // don't list hidden files
        
    ?>
        <tr>
            <td><a href="<?php echo $directory $dirArray[$index];?>"><?php echo $dirArray[$index];?></a></td>
            <td><?php echo filetype($directory $dirArray[$index]);?></td>
            <td><?php echo filesize($directory $dirArray[$index]);?></td>
        </tr>
    <?php
        
    }
    }
    ?>
    </table>
    Corrections to my coding/thoughts welcome.

  4. The Following User Says Thank You to bluewalrus For This Useful Post:

    arsenalbates (02-17-2011)

  5. #4
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I am using nearly identical script - question:

    how would I modify the code to exclude index.php as well?

    thanks!

  6. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    At this line

    PHP Code:
    if (substr("$dirArray[$index]"01) != "."){ 
    You can add in restrictions

    PHP Code:
    if (substr("$dirArray[$index]"01) != "."){
    if (
    $dirArray[$index] != "index.php") {
    //close it at the right location too 
    Not sure what those double quotes are there either, you could remove them to

    PHP Code:
    if (substr($dirArray[$index], 01) != "."){ 
    Corrections to my coding/thoughts welcome.

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
  •