我正在做一个小画廊。我想从目录中读取文件名,并在删除一些前导数字和文件扩展名后打印下面的文件名。
我有两个版本的代码。
版本 1 不排序
$current_dir = "$DOCUMENT_ROOT"."/weddings2/";
$dir = opendir($current_dir); // Open the sucker
while ($file = readdir($dir)) // while loop
{
$parts = explode(".", $file); // pull apart the name and dissect by period
if (is_array($parts) && count($parts) > 1) { // does the dissected array have more than one part
$extension = end($parts); // set to we can see last file extension
$bfile= substr($file, 2); //strips the first two characters
$cfile= preg_replace(('/\d/'),' ',$bfile);//remove numbers
$cfile= preg_replace(('/_/'),' ',$cfile);
$cfile= preg_replace(('/.jpg/'),' ',$cfile);
if ($extension == "jpg" OR $extension == "JPG") // is extension ext or EXT ?
echo "<td><img src=\"weddings2/$file\"><br />$cfile</td>\n";
}
}
closedir($dir); // Close the directory after we are done
版本 2 可以排序,但我无法操作文件名
$current_dir = "$DOCUMENT_ROOT"."/weddings2/";
$dir = opendir($current_dir); // Open the sucker
$files = array();
while ($files[] = readdir($dir));
sort($files);
closedir($dir);
foreach ($files as $file)
if ($file <> "." && $file <> ".." && !preg_match("/^hide/i",$file))
$table_cell .= "<td><img src='".'weddings2/'. rawurlencode($file) ."'><br />$cfile</td>\n";
echo $table_cell;
是的,我知道我很蠢。啊啊!
EDIT: 您的代码缺少大括号
你有
foreach (...)
code
code
应该是
foreach (...) {
code
code
}
只需将代码放在 $parts 和 foreach 循环后的最后一个 $cfile 之间,只需在循环中添加大括号,以便可以放入更多代码。另请注意,两个代码片段中的 if 条件不同,您必须决定使用哪个一个使用 或 if 将它们组合成一个条件。
$current_dir = "$DOCUMENT_ROOT"."/weddings2/";
$dir = opendir($current_dir); // Open the sucker
$files = array();
while ($files[] = readdir($dir));
sort($files);
closedir($dir);
foreach ($files as $file) {
//MANIPULATE FILENAME HERE, YOU HAVE $file...
if ($file <> "." && $file <> ".." && !preg_match("/^hide/i",$file))
echo "<td><img src='".'weddings2/'. rawurlencode($file) ."'><br />$cfile</td>\n";
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)