ファイル名を2つに分割して、phpなどを使用してループ経由でアクセスしたい。image_apple.jpg、image_mango.jpg、image_grapes.jpg など
各画像ファイルに対応する記述ファイルがあります
例: description_apple.txt
これらのファイルはすべて同じフォルダーにあります。
すべての画像と対応する説明ファイルを Web ページに表示したい
誰か助けてください よろしくお願いします
次のようなものを使用できます。
<?php
$files = scandir("./"); //./ is the current directory
foreach($files as $file) {
$info = explode('_', $file); //Split on _
//Skip all files withouth description
if (count($info) <= 1) {
continue;
}
$info = explode('.', $info['1']); //Split on .
//Is there a file with description info?
if (file_exists('description_' . $info['0'] . '.txt')) {
$description = file_get_contents('description_' . $info['0'] . '.txt'); //$description contains the file description
}
}
?>
これを試して
if ($handle = opendir('/folder path here/')) {
while (false !== ($entry = readdir($handle))) {
$names=explode("_",pathinfo($entry)['filename']);
echo " desc: description_".$names[0].".txt";echo " name: image_".$names[1].".jpg";
echo "<br>";
}
closedir($handle);
}