Question

Here is my current code (that i am very proud of for my first attempt at php)

<?php
$files = glob("/jobops/*.txt");
$indexcount = count($files);
sort($files);
print("<br>"+$indexcount+"<br>");
foreach ($files as &$file)
{
    print("<a href=\"$file\">$file</a><br>");
}
?>

the problem is glob again works fine in the root directory (where this script is located) but when i point it to a specific folder it returns 0 files found.

I've included a screen grab from my ftp client showing the directory structure (below)

so I'm confused what I'm doing wrong on the glob

www.markonsolutions.com/test.php is where the script is and if you click it will return 0 files found

alt text

Was it helpful?

Solution

"/jobops/*.txt"

is an absolute path on *nix, if you want to point to the directory that is in the same directory as a your php script you need to use:

"jobops/*.txt"

which is a relative path.

OTHER TIPS

As said

"/jobops/*.txt"

is an absolute path since it starts with a "/" (slash)

Relative paths are those not starting with a slash, so

"jobops/*.txt"

"./jobops/*.txt"

"../jobops/*.txt"

are all relative ... the point is: relative to what?

On OSs it is relative to the directories enumerated in the environment variable PATH (and often the current working dir is the first one listed into this variable, leding to the erroneous guess that "jobops/*.txt" is referred to the jabops dir in the current dir!)

In PHP, that is you environment, it is better to always use absolute paths, which does not implies to hard code them of course.

I suggest to do the following:

dirname(__FILE__).'/jobops/*.txt'

which is how to use paths in PHP, correctly

maybe something like:

$files = glob($_SERVER['DOCUMENT_ROOT']."/jobops/*.txt");

If you are running this from a web server? Could it be the User and Group settings of the web server will not allow access to other directories? Is this a permissions issue? Check the web server's error_log and access_logs for clues.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top