0

画像を表示する Web ページを取得しようとしていますが、機能していないようです。

コードは次のとおりです。

    <?php
$files = glob("images/*.*");
for ($i=1; $i<count($files); $i++)
{
    $num = $files[$i];
    echo '<img src="'.$num.'" alt="random image">'."&nbsp;&nbsp;";
    }
?>

コードが機能する場合、どこに配置すればよいですか? そうでない場合、これを行うより良い方法はありますか?

4

4 に答える 4

3

このコードは、「images」という名前のディレクトリを含むディレクトリに配置する必要があります。*.*「images」という名前のディレクトリにも、名前形式のファイルが必要です。あなたがやろうとしていることを行うためのより良い方法は間違いなくあります。これは、表示したいすべての画像を含むデータベースを使用することです。

それがあなたのやりたいことに合わない場合は、もっと説明的でなければなりません。あなたが何をしたいのかわかりません。あなたが示したコードから得られるのは、「images」というディレクトリ内のすべてのファイルを画像としてレンダリングすることだけです。

ただし、この投稿のこの時点で単に「PHP を実行するにはどうすればよいですか?」と尋ねる場合は、検索を行ってください。そのような質問で私たちを悩ませることはありません。

@zerkms が気付いたもう 1 つのことは、for ..ループが反復 1 ( $i = 1) で開始されることです。これは、配列内の結果がスキップされることを意味します。

for ($i = 0; $i < count($files); $i++) {
于 2012-10-29T01:32:20.937 に答える
1

<img>このコード スニペットは、ディレクトリ images/ 内のファイルを反復処理し、タグでラップされたファイル名をエコーし​​ます。イメージしたい場所に置いてみませんか?

于 2012-10-29T01:32:40.647 に答える
1

これは、画像をリストするフォルダーの親ディレクトリにある PHP ファイル (images.phpたとえば) に入ります。次の構文を使用してimages、ループを単純化することもできます (配列インデックスは0ではなくから開始する必要があるため、ループを修正します)。1

<?php
foreach (glob("images/*.*") as $file){
    echo '<img src="'.$file.'" alt="random image">&nbsp;&nbsp;';
}
?>
于 2012-10-29T01:36:39.600 に答える
0
/**
* Lists images in any folder as long as it's inside your $_SERVER["DOCUMENT_ROOT"].
* If it's outside, it's not accessible.
* Returns false and warning or array() like this:
* 
* <code>
* array('/relative/image/path' => '/absolute/image/path');
* </code>
* 
* @param string $Path
* @return array/bool
*/
function ListImageAnywhere($Path){
    // $Path must be a string.
    if(!is_string($Path) or !strlen($Path = trim($Path))){
        trigger_error('$Path must be a non-empty trimmed string.', E_USER_WARNING);
        return false;
    }
    // If $Path is file but not folder, get the dirname().
    if(is_file($Path) and !is_dir($Path)){
        $Path = dirname($Path);
    }
    // $Path must be a folder.
    if(!is_dir($Path)){
        trigger_error('$Path folder does not exist.', E_USER_WARNING);
        return false;
    }
    // Get the Real path to make sure they are Parent and Child.
    $Path = realpath($Path);
    $DocumentRoot = realpath($_SERVER['DOCUMENT_ROOT']);
    // $Path must be inside $DocumentRoot to make your images accessible.
    if(strpos($Path, $DocumentRoot) !== 0){
        trigger_error('$Path folder does not reside in $_SERVER["DOCUMENT_ROOT"].', E_USER_WARNING);
        return false;
    }
    // Get the Relative URI of the $Path base like: /image
    $RelativePath = substr($Path, strlen($DocumentRoot));
    if(empty($RelativePath)){
        // If empty $DocumentRoot ===  $Path so / will suffice
        $RelativePath = DIRECTORY_SEPARATOR;
    }
    // Make sure path starts with / to avoid partial comparison of non-suffixed folder names
    if($RelativePath{0} != DIRECTORY_SEPARATOR){
        trigger_error('$Path folder does not reside in $_SERVER["DOCUMENT_ROOT"].', E_USER_WARNING);
        return false;
    }
    // replace \ with / in relative URI (Windows)
    $RelativePath = str_replace('\\', '/', $RelativePath);
    // List files in folder
    $Files = glob($Path . DIRECTORY_SEPARATOR . '*.*');
    // Keep images (change as you wish)
    $Files = preg_grep('~\\.(jpe?g|png|gif)$~i', $Files);
    // Make sure these are files and not folders named like images
    $Files = array_filter($Files, 'is_file');
    // No images found?!
    if(empty($Files)){
        return array(); // Empty array() is still a success
    }
    // Prepare images container
    $Images = array();
    // Loop paths and build Relative URIs
    foreach($Files as $File){
        $Images[$RelativePath.'/'.basename($File)] = $File;
    }
    // Done :)
    return $Images; // Easy-peasy, general solution!
}

// SAMPLE CODE COMES HERE
// If we have images...
if($Images = ListImageAnywhere(__FILE__)){ // <- works with __DIR__ or __FILE__
    // ... loop them...
    foreach($Images as $Relative => $Absolute){
        // ... and print IMG tags.
        echo '<img src="', $Relative, '" >', PHP_EOL;
    }
}elseif($Images === false){
    // Error
}else{
    // No error but no images
}

サイズはこちらをご試着ください。コメントは自明です。

于 2012-10-29T02:02:37.327 に答える