【问题标题】:How to access last file from folder in php如何从php中的文件夹访问最后一个文件
【发布时间】:2019-06-27 05:44:53
【问题描述】:

我有一个名为 images 的文件夹,我想从文件夹访问最后一个文件以及图像的命名,如 AB00001.png 以及第二个图像 ImageName-AB00002 等等。每当它到达 ImageName-AB000010 时,在这种情况下,将最后一个图像作为 ImageName-AB00009,而不是 ImageName-AB000010。

// scanning last file   
$files = scandir('../images/', SCANDIR_SORT_DESCENDING);
//for name of last file
echo $newest_file = $files[0];
// for substring after hyphen
$lclStrAfterHyphen = substr($newest_file, strpos($newest_file, "-") + 1);
// checking string length
echo strlen($lclStrAfterHyphen);

【问题讨论】:

  • 你删除帖子的原因。
  • 只是点击了错误的链接;)
  • 好的,现在如果图像名称长度为 5,则获取最后一张图像,但如果为 6,则获取长度为 5 的图像本身。
  • 图片名称长度是什么意思?前缀“ImageName-”总是相同还是不同?
  • 图片名称会不同。

标签: php


【解决方案1】:

按预期工作:

$files = scandir(__DIR__.'/files', SCANDIR_SORT_DESCENDING);
print_r($files);
/*
 * OUTPUT:
 * Array
 * (
 *    [0] => testfile010.txt
 *    [1] => testfile009.txt
 *    [2] => testfile008.txt
 *    [3] => testfile007.txt
 *    [4] => testfile006.txt
 *    [5] => testfile005.txt
 *    [6] => testfile004.txt
 *    [7] => testfile003.txt
 *    [8] => testfile002.txt
 *    [9] => testfile001.txt
 *    [10] => ..
 *    [11] => .
 */

【讨论】:

  • 如果图像像 testfile010.txt 工作如果包含额外的零像 testfile0010.txt 那么它不工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-06
  • 2015-07-04
  • 1970-01-01
  • 2018-12-28
  • 2017-06-17
  • 2021-08-24
  • 2012-08-06
相关资源
最近更新 更多