PHP を使用してフォルダー/ディレクトリから画像のタイトル (abc.jpg など) を取得し、それらを配列に格納するにはどうすればよいですか。
例えば:
a[0] = 'ac.jpg'
a[1] = 'zxy.gif'
等
スライド ショーで配列を使用します。
それは確かに可能です。opendir のドキュメントを見て、すべてのファイルを結果配列にプッシュしてください。PHP5 を使用している場合は、DirectoryIteratorをご覧ください。これは、ディレクトリの内容をトラバースするためのよりスムーズでクリーンな方法です!
編集: opendirでのビルド:
$dir = "/etc/php5/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
$images = array();
while (($file = readdir($dh)) !== false) {
if (!is_dir($dir.$file)) {
$images[] = $file;
}
}
closedir($dh);
print_r($images);
}
}
「scandir」はこれを行います:
$images = scandir($dir);
一発ギャグ :-
$arr = glob("*.{jpg,gif,png,bmp}", GLOB_BRACE)
glob in php -パターンに一致するパス名を見つける
<?php
//path to directory to scan
$directory = "../images/team/harry/";
//get all image files with a .jpg extension. This way you can add extension parser
$images = glob($directory . "{*.jpg,*.gif}", GLOB_BRACE);
$listImages=array();
foreach($images as $image){
$listImages=$image;
}
?>