从目录加载随机图像 - php

我想从目录中随机加载图像,并在某处有一个刷新整个页面的按钮。这是我现在拥有的当前代码:

<?php
$a = array();
$dir = '../public/wp-content/uploads/2012/01';
if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {
    if (preg_match("/\.png$/", $file)) $a[] = $file;
    elseif (preg_match("/\.jpg$/", $file)) $a[] = $file;
    elseif (preg_match("/\.jpeg$/", $file)) $a[] = $file;
  }
  closedir($handle);
}

foreach ($a as $i) {

  echo "<img src='" . $dir . '/' . $i . "' />";

}

?>  

问题在于它一次加载了所有40万张图像。我只想加载30个。目录中的30张随机图像。我尝试查找一些代码,例如将上面的内容修改为:

<?php
$a = array();
$dir = '../public/wp-content/uploads/2012/01';
if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {
    if (preg_match("/\.png$/", $file)) $a[] = $file;
    elseif (preg_match("/\.jpg$/", $file)) $a[] = $file;
    elseif (preg_match("/\.jpeg$/", $file)) $a[] = $file;
  }
  closedir($handle);
}

foreach ($a as $i) {

  echo "<img src='" . $dir . '/' . $i . "' />";
  if (++$i == 2) break;

}

?>  

但这似乎完全没有作用。.因此,如果有人可以帮助我从该目录中获取30张随机照片进行加载,并具有某种类型的重新加载按钮,那将是非常有帮助的。

先感谢您

参考方案

这是我的缓存解决方案:

<?php

define('CACHE_FILE', 'mycache.tmp');
define('CACHE_TIME', 20); // 20 seconds (for testing!)
define('IMG_COUNT', 30);
define('IMG_DIR', '../public/wp-content/uploads/2012/01');

/**
  * Loads the list (an array) from the cache
  * Returns FALSE if the file couldn't be opened or the cache was expired, otherwise the list (as an array) will be returned.
  */
function LoadListFromCache($cacheFile, $cacheTime)
{
  if ( file_exists($cacheFile) )
  {
    $fileHandle = fopen($cacheFile, 'r');
    if ( !$fileHandle )
      return false;

    // Read timestamp (separated by "\n" from the content)
    $timestamp = intval( fgets($fileHandle) );
    fclose($fileHandle);
    // Expired?
    if ( $timestamp+$cacheTime > time() )
      return false;
    else
    {
      // Unserialize the content!
      $content = file_get_contents($cacheFile);
      $content = substr( $content, strpos($content, "\n") );

      $list = unserialize($content);
      return $list;
    }
  }
  return false;
}

/**
  * Caches the passed array
  * Returns FALSE if the file couldn't be opened, otherwise TRUE.
  */
function SaveListToCache($cacheFile, $list)
{
  $fileHandle = fopen($cacheFile, 'w');
  if ( $fileHandle === FALSE ) return false;

  fwrite($fileHandle, time());
  fwrite($fileHandle, "\n");
  fwrite($fileHandle, serialize($list));

  fclose($fileHandle);
  return true;
}

/**
  * Generates the list of all image files (png, jpg, jpeg) and caches it.
  * Returns the list as an array.
  */
function GenerateList()
{
  $a = array();
  $dir = IMG_DIR;
  if ($handle = opendir($dir))
  {
    while (false !== ($file = readdir($handle)))
    {
      if (preg_match("/\.png$/", $file)) $a[] = $file;
      elseif (preg_match("/\.jpg$/", $file)) $a[] = $file;
      elseif (preg_match("/\.jpeg$/", $file)) $a[] = $file;
    }
    closedir($handle);
  }
  SaveListToCache(CACHE_FILE, $a);
  return $a;
}

function GetRandomImages($list, $count)
{
  $listCount = count($list);
  $randomEntries = array();

  for ($i=0; $i<$count; $i++)
  {
    $randomEntries[] = $list[ rand(0, $listCount) ];
  }
  return $randomEntries;
}

// This code will execute the other functions!

$list = LoadListFromCache(CACHE_FILE, CACHE_TIME);

if ( $list === FALSE )
{
  $list = GenerateList();
}
$images = GetRandomImages($list, IMG_COUNT);

foreach ($images as $image)
{
  echo '<img src="', IMG_DIR.DIRECTORY_SEPARATOR.$image, '" />';
}

如何在PHP中设置get_file_contents的时间限制? - php

有时get_file_contents花费的时间太长,这会挂起整个脚本。有什么方法可以设置get_file_contents的超时限制,而无需修改脚本的最大执行时间?编辑:由于该文件不存在,因此花费了很长时间。我收到“无法打开流:HTTP请求失败!”错误。但这需要永远。 参考方案 通过使用timeout option创建上下文,似乎在PHP> 5.2.…

获取PHP中函数调用者的__FILE__常量 - php

我知道PHP中的__FILE__魔术常数将变成当前执行文件的完整路径和文件名。但是,有什么方法可以使函数的调用文件获得相同的信息吗?例如://foo.php: include "bar.php"; call_it(); //bar.php function call_it() { echo "Calling file: …

PHP strtotime困境 - php

有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00�…

PHP-全局变量的性能和内存问题 - php

假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…

PHP-正则表达式删除引号并添加大括号? - php

好吧,我不愿承认这一点,但是我对REGEX感到很困难,我永远找不到关于如何设置表达式的不错的教程。所以说我有这样的事情context['something'] 我想将所有事件更改为context[something] 那我有' . $var . ' 我想将所有事件更改为{var} 这是当前的概念,但是我在正则表达式部分…