php doesn't scan relative path for files

2019-03-05 17:51发布

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 http://i30.tinypic.com/106alba.jpg

标签: php file glob
4条回答
神经病院院长
2楼-- · 2019-03-05 18:02
"/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.

查看更多
再贱就再见
3楼-- · 2019-03-05 18:09

maybe something like:

$files = glob($_SERVER['DOCUMENT_ROOT']."/jobops/*.txt");
查看更多
乱世女痞
4楼-- · 2019-03-05 18:17

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.

查看更多
别忘想泡老子
5楼-- · 2019-03-05 18:24

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

查看更多
登录 后发表回答