简体   繁体   English

PHP-显示目录中的4个最新图像

[英]PHP - Display 4 most recent images from directory

Very new to PHP. PHP的新手。 I have sucessfully got this piece of code working to display 4 images from a directory, however it shows the first 4 images by name (001.png, 002.png, 003.png and 004.png) which are the lowest numbers and happen to be the least recently uploaded: 我已经成功获得了此片的代码的工作从目录显示4张图像,但是它示出了第一图像4按名称(001.png,002.png,003.png和004.png),这是最低的号码和发生至少是最近上传的:

<?php
$pictures = glob("directory/*.png"); 
for( $i=0; $i<=3; $i++ ){ 
echo "<img src=\"".$pictures[$i]."\" />"; 
}  
?>

I am looking to change that to get the 4 most recently uploaded in a directory by name. 我希望更改它,以便按名称将最近上传的4个文件上传到目录中。 In other words, I would like to display the last 4 images with the highest number. 换句话说,我想显示具有最高编号的最后 4张图像。 I have tried this below however I am getting Parse error: syntax error, unexpected T_VARIABLE on line 4 我在下面尝试过此方法,但是我遇到了解析错误:语法错误,第4行出现意外的T_VARIABLE

<?php
$pictures = glob("directory/*.png"); 
$no_pictures = count($pictures)-1 
$limit = $no_pictures-3 
for( $i = $no_pictures; $i >= $limit; $i--; ){ 
echo "<img src=\"".$pictures[$i]."\" />\n"; 
}  
?>

Any help is appreciated. 任何帮助表示赞赏。 Thanks for your time. 谢谢你的时间。

You are missing the ending semicolons ; 您缺少结尾的分号; on lines 3 & 4 [edit] AND you have an extra one in your for loop after $i-- - 在第3行和第4行[edit]并且$i--之后的for循环中还有一个-

<?php
$pictures = glob("directory/*.png"); 
$no_pictures = count($pictures)-1;  // was missing ;
$limit = $no_pictures-3;            // was missing ;
for( $i = $no_pictures; $i >= $limit; $i--){  // removed ; after $i--
echo "<img src=\"".$pictures[$i]."\" />\n"; 
}  
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM