php glob 不工作了,怎么回事

2013-02-03   来源:站长日记       编辑:沧海桑田   类别:PHP 教程    转载到:    发表评论

php glob 不工作,是怎么回事? 在本地测试工作良好,上传服务器不工作了,$path = $_SERVER['DOCUMENT_ROOT'].'/data/html-Cache/'; foreach (glob($path.‘*',GLOB_NOESCAPE ) as $file){ echo $file ,'
'; }

//$path = $_SERVER['DOCUMENT_ROOT'].'/data/html-Cache/'; //不工作? 为什么呢?
$path = $_SERVER['DOCUMENT_ROOT'].'/data/htmlCache/'; //echo $path;
foreach (glob($path.‘*',GLOB_NOESCAPE ) as $file){  echo $file ,'
'; }
?>
//目录一般不要用 - 也就是中划线

php glob
(PHP 4 >= 4.3.0, PHP 5)

glob — 寻找与模式匹配的文件路径

说明
array glob ( string $pattern [, int $flags = 0 ] )
glob() 函数依照 libc glob() 函数使用的规则寻找所有与 pattern 匹配的文件路径,类似于一般 shells 所用的规则一样。不进行缩写扩展或参数替代。

参数

pattern
The pattern. No tilde expansion or parameter substitution is done.

flags
有效标记有:

■GLOB_MARK - 在每个返回的项目中加一个斜线
■GLOB_NOSORT - 按照文件在目录中出现的原始顺序返回(不排序)
■GLOB_NOCHECK - 如果没有文件匹配则返回用于搜索的模式
■GLOB_NOESCAPE - 反斜线不转义元字符
■GLOB_BRACE - 扩充 {a,b,c} 来匹配 'a','b' 或 'c'
■GLOB_ONLYDIR - 仅返回与模式匹配的目录项
■GLOB_ERR - 停止并读取错误信息(比如说不可读的目录),默认的情况下忽略所有错误

注释
Note: 此函数不能作用于远程文件,被检查的文件必须是可通过服务器的文件系统访问的。

Note: 此函数在一些系统上还不能工作(例如一些旧的 Sun OS)。

Note: GLOB_BRACE 在一些非 GNU 系统上无效,比如 Solaris。

如果一个目录下有很多文件的话
Don't use glob() if you try to list files in a directory where very much files are stored (>100.000). You get an "Allowed memory size of XYZ bytes exhausted ..." error.
You may try to increase the memory_limit variable in php.ini. Mine has 128MB set and the script will still reach this limit while glob()ing over 500.000 files.

The more stable way is to use readdir() on very large numbers of files:
// code snippet
if ($handle = opendir($path)) {
    while (false !== ($file = readdir($handle))) {
        // do something with the file
        // note that '.' and '..' is returned even
    }
    closedir($handle);
}
?>
代替方案
A simple function that find all files by extension an return it by an array.
function findFiles($directory, $extensions = array()) {
    function glob_recursive($directory, &$directories = array()) {
        foreach(glob($directory, GLOB_ONLYDIR | GLOB_NOSORT) as $folder) {
            $directories[] = $folder;
            glob_recursive("{$folder}/*", $directories);
        }
    }
    glob_recursive($directory, $directories);
    $files = array ();
    foreach($directories as $directory) {
        foreach($extensions as $extension) {
            foreach(glob("{$directory}/*.{$extension}") as $file) {
                $files[$extension][] = $file;
            }
        }
    }
    return $files;
}
var_dump(findFiles("C:", array (

    "jpg",
    "pdf",
    "png",
    "html"
)));
?>

9

2
9|2 | 鲜花 VS 砸蛋 | 55阅读 0评论