0

特定の方法で終了するディレクトリに最後に変更された画像を表示したいと思います。

ディレクトリ内の最後のファイルを表示するように機能しています。結果をフィルタリングして、「wide-block.jpg」で終わるディレクトリ内の最新のファイルのみを表示する方法がわかりません。

<?php
$base_url = 'images/folio/web-portfolio';
$newest_mtime = 0;
$show_file = 'images/folio/no-image.jpg';
if ($handle = opendir($base_url)) {
while (false !== ($latestFile = readdir($handle))) {
    if (($latestFile != '.') && ($latestFile != '..') && ($latestFile != '.htaccess')) {
       $mtime = filemtime("$base_url/$latestFile");
       if ($mtime > $newest_mtime) {
          $newest_mtime = $mtime;
          $show_file = "$base_url/$latestFile";
       }
    }
  }
}
echo '<img src="' .$show_file. '" alt="Latest from the web">';
?>

私はそのコードをどこかから入手しましたが、「wide-block.jpg」で終わる最新のファイルを表示するチェックを行った後、!=ファイルタイプの2番目のifステートメントは必要ないことに気付きました。

4

2 に答える 2

1

これで整理できます。基本的には、必須フィールドで終わる画像をフィルタリングするための別のルールを追加するだけです。

基本的には、strpos()関数を使用して、探している値がファイル名に含まれているかどうかを判断します。

また、関数内でコードをラップし、ボットが目的のフォルダーの場所と、ファイルが終了する必要のある目的の文字列を許可できるようにします。

<?php
function getLatestImage($folderName, $imageEnding) {
    $newest_mtime = 0;
    $base_url = 'images/folio/'.$folderName;        
    $file_ending = $imageEnding;
    $show_file = 'images/folio/no-image.jpg';
    if ($handle = opendir($base_url)) {
        while (false !== ($latestFile = readdir($handle))) {
            if (($latestFile != '.') && ($latestFile != '..') && ($latestFile != '.htaccess') && (strpos($latestFile, $file_ending))) {
             $mtime = filemtime("$base_url/$latestFile");
                if ($mtime > $newest_mtime) {
                    $newest_mtime = $mtime;
                    $show_file = "$base_url/$latestFile";
                }
            }
        }
    }
    return $show_file;
}

echo '<img src="' .getLatestImage('foldername', 'file_ending.jpg'). '" alt="Latest from the web">';
?>
于 2012-08-29T18:56:05.103 に答える
0

それが私のやり方です。この問題を解決します。私はプログラマーではありません。エラー製品を試してみます:)

私はそれをワードプレスのインストールで使用し、プラグインAllow PHPを投稿とページにインストールする必要がありました (そのため、通常は[PHP] [/ PHP]ではなくphpタグが異なります)


[PHP]
global $path;    // path to directory has to be global, using it later in html

$dir = "Planetwebcam/";

$files = scandir($dir, 1);                // read directory backkwards

$picture = ($files[0]);                   // get las picture file name

$path = "http://www.yourdomain.ch/Planetwebcam/$picture";

[/PHP]


<a href="[PHP]global $path;echo $path; [/PHP]">
<img class="alignnone size-large" alt="Last picture" src="[PHP]global 
$path;echo $path; [/PHP]" width="1600" height="1200" /></a>
于 2013-07-09T07:39:54.270 に答える