0

我有以下代码读取文件夹的内容

PHP代码:

<?
//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

?>

它吐出以下代码:

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";​

现在在我的 html 文件中,我想回显类似的内容

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

我怎样才能做到这一点?

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($array); 或者,如果您想要格式良好的数组,那么

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

2)用于var_dump($array)获取数组中内容的更多信息,如数据类型和长度。

3) 你可以使用 php 的 foreach() 循环数组;并获得所需的输出。php 文档网站http://in3.php.net/manual/en/control-structures.foreach.php中有关 foreach 的更多信息

于 2014-06-08T15:33:05.430 回答