Jump to content


Photo

loading files


  • Please log in to reply
4 replies to this topic

#1 taith

taith
  • Members
  • PipPipPip
  • Advanced Member
  • 1,514 posts
  • LocationCanada

Posted 14 March 2006 - 08:58 PM

looking for a piece of code... that can look at all the files in a specified directory and list them by file name...
the code must use a with statement and a variable for the filename... any help?
90% of computer issues exist between the seat, and the keyboard.

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 14 March 2006 - 09:27 PM

[!--quoteo(post=355047:date=Mar 14 2006, 03:58 PM:name=Taith)--][div class=\'quotetop\']QUOTE(Taith @ Mar 14 2006, 03:58 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
looking for a piece of code... that can look at all the files in a specified directory and list them by file name...
the code must use a with statement and a variable for the filename... any help?
[/quote]

welcome to the forums! i hope you find lots of great help here.

i'm not sure i understand the second part of your question. it is fairly simple to loop through a directory and list the files:
function getMyFiles($dir) {
  if ($handle = opendir($dir)) {
    $files = array();
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
      $files[] = $file;
    }

    closedir($handle);
  }
  sort($files);
  $out = '';
  foreach ($files as $file)
    $out .= "$file<br />\n";

  return $out;
}

echo getMyFiles("path/to/files");

if you need further instruction, or if this isn't what you're after, please try to explain further.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 taith

taith
  • Members
  • PipPipPip
  • Advanced Member
  • 1,514 posts
  • LocationCanada

Posted 14 March 2006 - 10:44 PM

exaclty what i need... but that one doesnt do anything...
90% of computer issues exist between the seat, and the keyboard.

#4 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 14 March 2006 - 11:36 PM

[!--quoteo(post=355105:date=Mar 14 2006, 10:44 PM:name=Taith)--][div class=\'quotetop\']QUOTE(Taith @ Mar 14 2006, 10:44 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
exaclty what i need... but that one doesnt do anything...
[/quote]


Yes it does.. did you call the function??

<?php
$x = getMyFiles("/etc/");

print $x;
?>

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 15 March 2006 - 01:09 PM

[!--quoteo(post=355129:date=Mar 14 2006, 06:36 PM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 14 2006, 06:36 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Yes it does.. did you call the function??
[/quote]

like keeB said, it should work just fine. make sure that you have a valid directory path you're using. remember that if you're going from root, you need the "/" at the beginning, but if you're just going from a relative folder, just write the path like i've shown above. here's a slight modification that will do another little error checking to make sure that you've declared a valid path:
function getMyFiles($dir) {
  if (is_dir($dir)) {
    if ($handle = opendir($dir)) {
      $files = array();
      echo "Directory handle: $handle\n";
      echo "Files:\n";

      /* This is the correct way to loop over the directory. */
      while (false !== ($file = readdir($handle))) {
        $files[] = $file;
      }

      closedir($handle);
    }
    sort($files);
    $out = '';
    foreach ($files as $file)
      $out .= "$file<br />\n";
  } else {
    $out = "<p>Specified directory does not exist</p>\n";
  }
 
  return $out;
}

echo getMyFiles("path/to/files");

basically, i've added an is_dir() check to make sure that the designated directory exists before running the rest of the function.

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users