0

I have the following code that reads the content of a folder

PHP Code:

<?
//PHP SCRIPT: getimages.php
header('content-type: application/x-javascript');

//This function gets the file names of all images in the current directory
//and ouputs them as a JavaScript array
function returnimages($dirname="./images") {
     $pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions
     $files = array();
     $curimage=0;
           if($handle = opendir($dirname)) {
                while(false !== ($file = readdir($handle))){
                     if(eregi($pattern, $file)){ //if this file is a valid image 
//Output it as a JavaScript array element
                   echo 'galleryarray['.$curimage.']="'.$file .'";' . "\n";
             $curimage++;
                    } 
            }

            closedir($handle);
}
        sort($files);
        return($files);
}

echo 'var galleryarray=new Array();' . "\n"; //Define array in JavaScript
returnimages() //Output the array elements containing the image file names

?>

which spits out this the following code:

var galleryarray = new Array();
galleryarray[0] = "image1.jpg";
galleryarray[1] = "image5.jpg";
galleryarray[2] = "image2.jpg";
galleryarray[3] = "image4.jpg";
galleryarray[4] = "image8.jpg";
galleryarray[5] = "image7.jpg";
galleryarray[6] = "image0.jpg";
galleryarray[7] = "image3.jpg";
galleryarray[8] = "image6.jpg";​

Now in my html file I want to echo something like

<img src="images/image0.jpg" />
<img src="images/image1.jpg"/>
...
... 

How can I do that?

4

4 に答える 4

1
foreach($galleryarray as $img) {
    echo "<img src='images/$img' />";
}
于 2012-04-12T08:05:19.547 に答える
0

関数のエコーラインを次のように変更してみてください。

echo '<img src="images/' . $file . '" />' ;

これがお役に立てば幸いです:)

また

関数の外では、次のように使用します。

$images = returnimages(); //will get the array containing the images
foreach($images as $img)
{
  echo '<img src="images/' . $img . '" />';
}

また、whileループのif条件内にこの行を含める必要があります。

$files[] = $file; //insert the file into the array. This array is what we are going to return from the function

アップデート

私はこのコードをテストしました、そしてそれは働いています:

    //PHP SCRIPT: getimages.php
    header('content-type: application/x-javascript');

    function returnimages($dirname="./images") {
         $pattern="([^\s]+(\.(?i)(jpg|png|gif|bmp))$)";     //  http://www.mkyong.com/regular-expressions/how-to-validate-image-file-extension-with-regular-expression/
         $files = array();
         if($handle = opendir($dirname)) {
            while(false !== ($file = readdir($handle))){
                if(preg_match($pattern, $file)){ //if this file is a valid image 
                    $files[] = $file;
                } 
            }

            closedir($handle);
        }
        //sort($files);         // http://php.net/manual/en/function.sort.php
        natcasesort($files);    // case insensitive "natural order" algorithm :: http://php.net/manual/en/function.natcasesort.php

        return($files);
    }

    $images = returnimages(); //will get the array containing the images
    foreach($images as $img)
    {
      echo '<img src="images/' . $img . '" />';
    }

私のimagesフォルダには、テスト用に5つのファイルを入れました。そしてそれを実行すると、次のようになります。

<img src="images/a.jpg" />
<img src="images/ba.jpg" />
<img src="images/ca.jpg" />
<img src="images/Copy (3) of a.jpg" />
<img src="images/Copy (4) of a.jpg" />
于 2012-04-12T08:07:43.087 に答える
0

多分これはあなたを助ける

$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions
           if($handle = opendir("./images")) {
                while(false !== ($file = readdir($handle))){
                     if(eregi($pattern, $file)){ ?>
                   <img src="/images/<?php echo $file;  ?>">
            <?php 
                    } 
            }
           }
于 2012-04-12T09:52:32.227 に答える
0

画像名を値で並べ替えてから画像を表示するとします。まず、関数「asort」を使用して値を並べ替える必要があります

    asort($galleryarray);
    foreach ($galleryarray as $key => $val) {
        echo "<img src='images/{$val}'/>"
    }

印刷して配列したい場合

1) print_r($配列); または適切にフォーマットされた配列が必要な場合は、

echo '<pre>'; print_r($array); echo '<pre/>';

2)var_dump($array)データ型や長さなど、配列内のコンテンツの詳細情報を取得するために使用します。

3) PHP の foreach(); を使用して配列をループできます。目的の出力を取得します。foreach の詳細については、php のドキュメント Web サイトhttp://in3.php.net/manual/en/control-structures.foreach.phpを参照してください。

于 2014-06-08T15:33:05.430 に答える