Macworld Forums: PHP getting filelist of current directory - Macworld Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

PHP getting filelist of current directory

#1 User is offline   wattsup Icon

  • Member
  • PipPip
  • Group: Members
  • Posts: 39
  • Joined: 08-October 03

Posted 13 September 2006 - 03:37 PM

Can someone explain why this code does not work for the mac. This script works great on my site (can see here http://wattsupnow.com/dlist.php). Any help would be great!!!
<?php
// get current Directory
$myDir = getcwd() ;
//print directory path
echo $myDir;
// open directory and read all files
if ($handleDir = opendir($myDir)) {
// zero out counter
$i = 0;
//loop through all files in directory
while ($myfilenames = readdir($handleDir)) {
// add all files to an array
$myArray[$i] = $myfilenames;
// increase array element
$i;
// count total files in array
$arrayLength = count($myArray);
// loop through and print each element assign to array
for ($i = 0; $i < $arrayLength; $i){
// print each element
echo "myArray at" . $i . " is: " .$myArray[$i] . "<br>n";
}
// close current direct
closedir($handleDir);
}
// end script
?>
0

#2 User is offline   Nobody Icon

  • Power User
  • PipPipPipPip
  • Group: Members
  • Posts: 58,347
  • Joined: 18-October 07

Posted 14 September 2006 - 06:25 PM

Your problem mostly has to do with the curly braces in the wrong place. I've cleaned up and fixed your issues and here's the result:
Code:

<?php
$myDir = getcwd();
if ($handleDir = opendir($myDir))
{
while ($myfilenames = readdir($handleDir)) // loop through all files in $myDir
$myArray[] = $myfilenames; // add all file to $myArray
$arrayLength = count($myArray); // count total files in $myArray
$i = 0;
foreach ($myArray as $file )
echo "myArray at" . $i . " is: " . $file . "<br />" . "n"; // print each $file in $myArray
closedir($handleDir); // close $myDir
}
?>


0

#3 User is offline   wattsup Icon

  • Member
  • PipPip
  • Group: Members
  • Posts: 39
  • Joined: 08-October 03

Posted 14 September 2006 - 08:40 PM

Thank You!!! That fixed everything!! I see what I was doing wrong.
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users