我有以下代码来读取文件夹的内容
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"/>
...
...
我该怎么做?
答案 0 :(得分:1)
foreach($galleryarray as $img) {
echo "<img src='images/$img' />";
}
答案 1 :(得分: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" />
答案 2 :(得分: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
}
}
}
答案 3 :(得分: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()循环数组;并获得所需的输出。有关foreach的更多信息,请访问php的文档网站http://in3.php.net/manual/en/control-structures.foreach.php