如何将服务器(最新最旧)的图像显示到新的php页面?

时间:2013-10-27 09:37:18

标签: php

我有一个创建图像的表单(使用imagejpeg,imagettftext和imagecreatefromjpeg)并将其保存在服务器的文件夹中。我需要做的是将所有创建的图像显示到另一个页面,最新的位于顶部,以便最旧的图像位于底部。我有一个名为formdisplay.php的表单,但它只显示一个损坏的图像,而不是最新的到最旧的。希望你能帮助我。真的需要让这个工作。提前感谢您的帮助。

我已经阅读了这些帖子,但没有一个对我有用。

Pull dedicated images from folder - show image + filename (strip part of filename + file-extension)

How can I display latest uploaded image first? (PHP+CSS)

getting images from the server and display them

Displaying images from folder in php

display image from server embedding php in html

formcreatesave.php

    imagecopymerge($im, $img2, 10, 350, 0, 0, imagesx($img2), imagesy($img2), 100);

$date_created = date("YmdHis");//get date created
$img_name = "-img_entry.jpg"; //the file name of the generated image
$img_newname = $date_created . $img_name; //datecreated+name
$img_dir =dirname($_SERVER['SCRIPT_FILENAME']) ."/". $img_newname; //the location to save 
imagejpeg($im, $img_dir , 80); //function to save the image with the name and quality

imagedestroy($im);

formdisplay.php

$dir = '/home3/site/public_html/Master/uploader/uploader';
$base_url = 'http://mysite.com/Master/uploader/uploader/20131027024705-img_entry.jpg';
$newest_mtime = 0;
$show_file = 'BROKEN';
if ($handle = opendir($dir)) {
 while (false !== ($file = readdir($handle))) {
    if (($file != '.') && ($file != '..')) {
       $mtime = filemtime("$dir/$file");
       if ($mtime > $newest_mtime) {
          $newest_mtime = $mtime;
          $show_file = "$base_url/$file";
       }
    }
  }
}
print '<img src="' .$show_file. '" alt="Image Title Here">';

请随时编辑我的代码。谢谢:))

2 个答案:

答案 0 :(得分:0)

  1. 从文件夹中读取图像。
  2. 循环遍历它们并使用图像的密钥文件时间插入到数组中。
  3. 使用krsort函数根据日期对它们进行排序。
  4. 循环并显示它们。
  5. 我认为此链接可以帮助您: http://forums.phpfreaks.com/topic/219466-php-displaying-images-from-folder-with-most-recent-first/

答案 1 :(得分:0)

这是在创建$ images数组的while循环之后(图像文件名列表      

 foreach ($images as $image)

 {

 $filetime = filemtime("images1/$image");

 $sortedimages[]$filetime => $image;

 }

 krsort($sortedimages);


 echo "<pre>";

 print_r($sortedimages);

 echo "</pre>"

 ?>