Jump to content

loading files


taith

Recommended Posts

[!--quoteo(post=355047:date=Mar 14 2006, 03:58 PM:name=Taith)--][div class=\'quotetop\']QUOTE(Taith @ Mar 14 2006, 03:58 PM) [snapback]355047[/snapback][/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:
[code]
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");
[/code]

if you need further instruction, or if this isn't what you're after, please try to explain further.
[!--quoteo(post=355105:date=Mar 14 2006, 10:44 PM:name=Taith)--][div class=\'quotetop\']QUOTE(Taith @ Mar 14 2006, 10:44 PM) [snapback]355105[/snapback][/div][div class=\'quotemain\'][!--quotec--]
exaclty what i need... but that one doesnt do anything...
[/quote]


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

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

print $x;
?>[/code]
[!--quoteo(post=355129:date=Mar 14 2006, 06:36 PM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 14 2006, 06:36 PM) [snapback]355129[/snapback][/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:
[code]
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");
[/code]

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.