Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php查找文件名中具有最高值的文件_Php_Filesystems_Glob - Fatal编程技术网

php查找文件名中具有最高值的文件

php查找文件名中具有最高值的文件,php,filesystems,glob,Php,Filesystems,Glob,考虑以下文件结构: /folder/locaux-S04_3.html /folder/blurb.txt /folder/locaux-S04_2.html /folder/locaux-S05_1.html /folder/tarata.02.jpg /folder/locaux-S04_1.html /folder/dfdsf.pdf 我需要检索目录中名称包含最高数值的文件。 在上面的示例中,它是locaux-S05_1.html 我提出glob()是一种只获取locaux-S*.htm

考虑以下文件结构:

/folder/locaux-S04_3.html
/folder/blurb.txt
/folder/locaux-S04_2.html
/folder/locaux-S05_1.html
/folder/tarata.02.jpg
/folder/locaux-S04_1.html
/folder/dfdsf.pdf
我需要检索目录中名称包含最高数值的文件。 在上面的示例中,它是locaux-S05_1.html

我提出glob()是一种只获取locaux-S*.html文件的有效方法,但我陷入了下一步:查找文件名包含最高值的文件

$files= glob(LOCAUX_FILE_PATH.'/locaux-S*.html');

foreach($files as $key=> $value){
    // loop through and get the value in the filename. Highest wins a trip to download land!

$end = strrpos($value,'.');
$len= strlen($value);
$length = $len-$end;
$str = substr($value,8,$length);
// this gives me the meat, ex: 03_02. What next?

}
任何指针都将不胜感激。

试试这个:

$files = glob(LOCAUX_FILE_PATH.'/locaux-S*.html');
$to_sort = array();

foreach ($files as $filename)
{
    if (preg_match('/locaux-S(\d+)_(\d+)\.html/', $filename, $matches)) {
        $to_sort[$matches[1].'.'.$matches[2]] = $filename;
    }
}

krsort($to_sort);
echo reset($to_sort); // Full filepath of locaux-S05_1.html in your example
我对排序方法不满意,也许有人可以在此基础上进行构建,因为不能将浮点用作数组键(它们被转换为整数,这是不好的)。我还假设您希望它们按照下划线之前的数字进行排序,然后使用第二个数字作为二级顺序标准。

尝试以下操作:

$files = glob(LOCAUX_FILE_PATH.'/locaux-S*.html');
$to_sort = array();

foreach ($files as $filename)
{
    if (preg_match('/locaux-S(\d+)_(\d+)\.html/', $filename, $matches)) {
        $to_sort[$matches[1].'.'.$matches[2]] = $filename;
    }
}

krsort($to_sort);
echo reset($to_sort); // Full filepath of locaux-S05_1.html in your example
我对排序方法不满意,也许有人可以在此基础上进行构建,因为不能将浮点用作数组键(它们被转换为整数,这是不好的)。我还假设您希望它们按照下划线之前的数字进行排序,然后使用第二个数字作为二级标准。

我找到了一个更简单的方法:

$files= glob(LOCAUX_FILE_PATH.'/locaux-S*.html');
sort($files); // sort the files from lowest to highest, alphabetically
$file  = array_pop($files); // return the last element of the array
我找到了一个更简单的方法:

$files= glob(LOCAUX_FILE_PATH.'/locaux-S*.html');
sort($files); // sort the files from lowest to highest, alphabetically
$file  = array_pop($files); // return the last element of the array