First, about me and what I'm running.

I am fairly new to PHP, but not new to programming. I am doing this on a Mac running OS X Panther, Apache 1.3.33, PHP 5.1.2.

Second the code.


<?php

$dir = '.'; // Current directory
$list = '';
$dircont = scandir($dir); // Scan directory for dir-contents
foreach($dircont AS $item ) // Check each item in $dircont
    	if(is_file($item) && $item[0]!='.')
$list .= '<a href="'.$item.'">'.$item.'</a>'."<br />\n";
echo $list; // Display the list

?>

Third the problem.

The code above is supposed to list all files within the directory the file is located in. I am having a hard time getting the above code to work for a subdirectory. By assigning '.' to $dir I am successful in getting the files under the current directory my index.php is in to display, but I want it to list all the files in a subdirectory called php that I have put all my php work in.

So far I have tried many different combinations but the errors have fallen between these two things.

$dir = './php/';

results in the main directory's files being displayed, when I would have thought it would create an error.

$dir = 'http://localhost/~username/php/';

results in the following error-

Warning: scandir(http://localhost/~username/php/) [function.scandir]: failed to open dir: not implemented in /Users/username/Sites/index.php on line 17

Warning: scandir() [function.scandir]: (errno 22): Unknown error: 0 in /Users/username/Sites/index.php on line 17

Warning: Invalid argument supplied for foreach() in /Users/username/Sites/index.php on line 18

I am figuring that I either do not know how to assign a path correctly to a variable or I do not understand how to give scandir() what it needs.

Thank you for any and all replies.

Babelosopher

    Although the manual claims this functions works with URL's, it appears as though it doesn't.

    Then again, that shouldn't matter; if the directory you're trying to scan is on the server itself, why would you want to use a URL wrapper? Just scan the directory using a local path (either relative or absolute).

      OK, so I've been trying out different paths and I can get certain ones to work and others still error out. Basically my server files are in /Users/username/Sites. Inside Sites I have a php folder. So I have assigned-

      $dir = '/Users/username/Sites/php';
      

      And while it does not error out, I still cannot get the code to execute inside php, but rather it stops at Sites and executes there.

      What I am beginning to wonder is if later on in the code, my path assignment is being made irrelevant due to it not actually being used. In the foreach and part of the if conditional, what is

      $item[0]!='.'
      

      doing exactly? I'm unsure of the reasoning behind [0] and I don't get what '.' means exactly. Any ideas?

        $item[0]!='.' is checking that the first character of string $item is not a period (full stop), thus excluding any file that starts with a '.' such as ".htaccess", as well as special name '.' and '..' (current directory and parent directory).

          NogDog wrote:

          $item[0]!='.' is checking that the first character of string $item is not a period (full stop), thus excluding any file that starts with a '.' such as ".htaccess", as well as special name '.' and '..' (current directory and parent directory).

          Is [0] what is specifying for the first character?

            Thanks to everyone for the replies. What was actually the culprit, was the is_file. I had to specify as is_file("php/" . $item) since it defaults to the directory of the file running it.

              Write a Reply...