+ Reply to Thread
Results 1 to 2 of 2

Thread: Php directory help

  1. #1
    thenewprogrammer is offline x10Hosting Member thenewprogrammer is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    45

    Php directory help

    I am trying to take files from a folder and output them into table under categories "filename", "filetype", "filesize". The script works fine but when it outputs any other file besides basic files such as txt,php,html. When the files are jpg, .zip, etc. it will create error not being able to find the filetype or size. I am new to php so plz point out anything that i should change.

    better way to show the error. The files show but it wont say the filetype or filesize correctly unless its basic file

    http://likeftp.com/download.php


    Code:
    <?php
     
    // open this directory 
    $myDirectory = opendir("./files/photo/");
    // 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=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
    print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n");
    // loop through the array of files and print them all
    for($index=0; $index < $indexCount; $index++) {
            if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
      print("<TR><TD><a href=\"/files/photo/$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
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Php directory help

    The issue isn't file type, it's the path.
    Quote Originally Posted by thenewprogrammer View Post
    Code:
    <?php
    while($entryName = readdir($myDirectory)) {
     $dirArray[] = $entryName;
    }
    The values returned by readdir() are file names, not paths...

    Quote Originally Posted by thenewprogrammer View Post
    Code:
      print(filetype($dirArray[$index]));
      print("</td>");
      print("<td>");
      print(filesize($dirArray[$index]));
    so when you pass a value from $dirArray to filetype and filesize, you're passing just the name of the file. If there happens to be a file in the current directory with the same name, the call succeeds. You should put the code in a function with the directory path as a parameter.

    A foreach loop over $dirArray is simpler than a for loop:
    Code:
    foreach ($files as $file) {
        if ('.' != $file[0]) {
            ...
        }
    }
    Note you can also access individual characters in a string by using square brackets.
    Last edited by misson; 10-25-2009 at 12:31 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. Replies: 0
    Last Post: 12-24-2008, 03:59 PM
  4. Disk Full
    By digitaljit in forum Free Hosting
    Replies: 2
    Last Post: 11-10-2008, 06:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers