开发者

PHP script link the *last* files in a folder

The way I have organized the reports in a particular folder on my site is fairly straight forward. First they all have the date, in big endian mode. This means the most recent report should be at the bottom.

So, the reports are named like this

2011010开发者_JAVA技巧2China01
20110105China01

Does anybody know a trick to have PHP pick out the very first or last thing in the folder?


You can make a call to shell_exec to execute a shell command. If you're doing this in a Linux environment, your command can look like one of the following.

Get the first file alphabetically in the directory:

$result = shell_exec("ls | head -n 1");

Get the last file alphabetically in the directory:

$result = shell_exec("ls | tail -n 1");


Try using glob() to grab them all and then getting the last thing in the array returned.

$contents = glob('path/to/directory');
$file = $contents[count($contents) - 1];
echo $file;

To get the first one do this.

$contents = glob('path/to/directory');
$file = $contents[0];
echo $file;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜