2012-06-27 23 views
2

列出圖像(01.png)和說明(01.txt)如何顯示目錄中的圖像並獲取每張圖像的相應描述,並給出說明存在。從目錄

在目錄

//

01.png 
01.txt 
02.png 
03.png 
03.txt 
etc. 

顯示爲//

<img src="01.png"><br>This is the description from the text file named 01.txt 
<img src="02.png"><br> 
<img src="03.png"><br>This is the description from the text file named 03.txt 

我一直在尋尋覓覓,卻找不到任何東西,因此,如果有人可以點我正確的方向,將不勝感激。對於想要創建非常簡單的畫廊或圖像和名稱列表的人來說,這也是一件非常有用的事情。

在此先感謝!

+0

嘗試結賬水珠http://php.net/manual/fr/function.glob.php,可以幫助你。 –

+1

這從谷歌機返回了不少成果很快,只是在說:) – ZnArK

回答

1

你的問題有點混亂。

製作一個包含所有信息的數組。

$pics = array('img' => '01.png', 'text' => 'This is the description'); 

foreach($pics as $pic) { 
    echo '<img src="'.$pic['name'].'" alt="">' . $pic['text']; 
} 

所以,你必須把你的信息在一個數組或一個數據庫,否則你不能映射到應將描述你的形象。

當你想動態讀取文件夾時,它有點困難。

你可以看看readdirglob然後你可以讀取所有圖像獲取名稱並加載文本文件file_get_contents但我認爲它不是一個真正的高性能的方式。

0

代碼此處修改:http://php.net/manual/en/function.readdir.php

//path to directory to scan 
$directory = "../images/team/harry/"; 

//get all image files with a .jpg extension. 
$images = glob($directory . "*.jpg"); 

//print each file name 
foreach($images as $image) 
{ 
    print "<img src=\"$image\"><br>This is the description from the text file named $image"; 
} 

好了,所以這將不打印文本文件的內容,但我敢肯定,你可以進一步修改上面的代碼了,找出

YEP

+0

這並不動態地從一個'.txt'文件加載描述。 – nickb

+0

不,我不是租用,一些努力可能需要代表OP的 – ZnArK

+0

我想我得到-1試圖將作爲有益,因爲任何人都應該被預期爲編碼器.... :( – ZnArK

3

這是你要找什麼,說明必須從相應.txt文件進行動態捕捉:

$dir = './'; 
$files = glob($dir . '*.png'); 
foreach($files as $file) { 
    $filename = pathinfo($file, PATHINFO_FILENAME) . '.txt'; 
    $description = file_exists($filename) ? file_get_contents($filename) : ''; 
    echo '<img src="' . $file . '"><br>' . $description; 
} 

它能做什麼是使用glob()從給定的目錄($dir*.png文件抓取的數組。然後,爲每個圖像獲取圖像的文件名(因此01.png將爲01),並附加.txt以獲取描述文件的名稱。然後,如果描述文件存在,它將使用file_get_contents()將描述文件加載到$description變量中。然後輸出所需的HTML。

0

的ZnArKs代碼更新的版本,因爲他錯過了你想要的文件

//path to directory to scan 
$directory = "../images/team/harry/"; 

//get all image files with a .jpg extension. 
$images = glob($directory . "*.png"); 

//print each file name 
foreach($images as $image) 
{ 
    $textfile = substr($image, 0, -3) . "txt"; 

    echo "<img src='{$image}'><br/>"; 

    if(file_exists($textfile)) 
    { 
     echo file_get_contents($textfile) . "<br/>"; 
    } 
} 
+0

不能保證'.txt'文件存在。 – nickb

+0

好點的,更新的代碼仄文件是否存在第一 –

+0

WOW,這做到了! 完美。我試圖弄清楚其中一個較老的答案,這就爲我節省了一天的時間。它非常有意義,謝謝soooo多!!! – EPE

2

的內容我假設你在同一目錄下的PHP文件的圖片和文字文件。

您可以使用功能​​3210從目錄讀取所有圖像文件作爲數組,從而切斷文件擴展名(如'01。png'變成'01')並且將文件擴展名附加到字符串連接。

的工作代碼示例可能看起來像這樣:

<?php 
    $path_to_directory = './'; 
    $pics = glob($path_to_directory . '*.png'); 
    foreach($pics as $pic) 
    { 
     $pic = basename($pic, '.png'); // remove file extension 
     echo '<img src=\"{$pic}.png\"><br>'; 
     if(file_exists($pic . '.txt')) 
     { 
      echo file_get_contents("{$pic}.txt"); 
     } 
    } 

?> 

所以肯定會對這些功能一看:

快樂編碼。

+0

這不會做你認爲它:'$ PIC =基本名($ PIC);'此外,還有不能保證'.txt'文件存在。 – nickb

+0

謝謝尼布......你完全正確。修正了它 - 謝謝! – GeneSys