我正在尝试创建一个使用PHP浏览资产文件夹并作为一种相册系统的图库。下面的代码将文件检索为相册:
<?php
include_once("../PhpScript/RetAlbInfo.php");
$files = array_slice(scandir('../Assets/Gallery/'), 2);
for($albflcnt = 0;$albflcnt<count($files);$albflcnt++){
echo "<div style='...'><div class='albm' style='...' onmousedown='gotoalbum();albnm();'></div><p style='...'>".$files[$albflcnt]."</p></div>";
}
?>
问题是我无法找到从每个<p>
标记中获取文件夹名称的方法,并将其附加到网址,以便显示它的子文件夹。
答案 0 :(得分:0)
您可以将glob()与GLOB_ONLYDIR选项
一起使用<?php
foreach (glob("*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>
或
<?php
$dirs = array_filter(glob('*'), 'is_dir');
print_r( $dirs);
?>
答案 1 :(得分:0)
您可能想要RecursiveDirectoryIterator?不是100%肯定。这是一个基本的例子。有些部分可能相关,也可能不相关。这基本上建立了各种各样的面包屑系统,但可能有你正在寻找的部分:
<?php
# Create some defines in the root of the website if not already done
# This is your url
define('SITE_URL','http://www.example.com');
# This is the root path
define('BASE_DIR',__DIR__);
# This is where everyour files are stored
define('MEDIA_DIR',__DIR__.'/media/');
/*
** @description This is just a link builder, there may be some functionality you could use here
** @param $path [string] Relative path
** @param $pathStr [string] Absolute path
** @param $append [string | empty] This is just append to whatever page processes this script
*/
function createLinks($path,$pathStr,$append = '/index.php')
{
# Used for the link
$url = SITE_URL.$append;
# Explodes relative path
$arr = array_filter(explode('/',$path));
# Loop through to create break crumb
foreach($arr as $dir) {
$pathStr .= '/'.$dir;
$new[] = '<a href="'.$url.'?goto='.urlencode(base64_encode($pathStr)).'">/'.$dir.'</a>';
}
# Return final bread crumb
return implode('',$new);
}
# Decodes the path name, you should probably do a more secure encryption / decription method
function getPathFromRequest($path)
{
return base64_decode(urldecode($path));
}
# Set base absolute path
$core = MEDIA_DIR;
# Process the absolute path
if(!empty($_GET['goto'])) {
$base = str_replace('//','/',getPathFromRequest($_GET['goto']));
if(is_dir($base))
$base = $base.'/';
}
else
$base = $core;
# If is a directory, recurse and show the bread crumbs
if(is_dir($base)){
$skip = RecursiveDirectoryIterator::SKIP_DOTS;
$sFirst = RecursiveIteratorIterator::SELF_FIRST;
$dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($base,$skip),$sFirst);
# Loop through the file paths
foreach($dir as $file) {
$fname = $file->__toString();
if(is_file($fname)) {
$abs = str_replace($base,'',$fname);
$useType = (!empty($_GET['goto']))? $base : $core;
echo createLinks($abs,$useType).'<br />';
}
}
}
else {
# Redirect to file if is a file
if(is_file($base)) {
header('Location: '.SITE_URL.'/'.str_replace(BASE_DIR,'',$base));
exit;
}
}
浏览器中的基本视图为您提供了类似的东西(这只是来自我的目录):
/css/form.css
/css/styles.css
/images/bkg/bkg_white.png
/images/bkg/row_grad01.png
然后html源代码如下:
<a href="http://www.example.com/index.php?goto=L2Rh%123BB%bi123abBAjMyMC91c2dyLzMwMzAwNjc1aHR321BaBaaXRlc3QvbWV%3aaazcw%3D%3D">/css</a><a href="http://www.example.com/index.php?goto=L2Rh%123BB%bi123abBAjMyMC91c2dyLzMwMzAwNjc1aHR321BaBaaXRlc3QvbWV%3aaazcy9mb3JtLmNzcw%3D%3D">/form.css</a><br />
<a href="http://www.example.com/index.php?goto=L2Rh%123BB%bi123abBAjMyMC91c2dyLzMwMzAwNjc1aHR321BaBaaXRlc3QvbWV%3aaazcw%3D%3D">/css</a><a href="http://www.example.com/index.php?goto=L2Rh%123BB%bi123abBAjMyMC91c2dyLzMwMzAwNjc1aHR321BaBaaXRlc3QvbWV%3aaazcy9zdHlsZXMuY3Nz">/styles.css</a><br />
<a href="http://www.example.com/index.php?goto=L2Rh%123BB%bi123abBAjMyMC91c2dyLzMwMzAwNjc1aHR321BaBaaXRlc3QvbWVkaWEvL2ltYWdlcw%3D%3D">/images</a><a href="http://www.example.com/index.php?goto=L2Rh%123BB%bi123abBAjMyMC91c2dyLzMwMzAwNjc1aHR321BaBaaXRlc3QvbWVkaWEvL2ltYWdlcy9ia2c%3D">/bkg</a><a href="http://www.example.com/index.php?goto=L2Rh%123BB%bi123abBAjMyMC91c2dyLzMwMzAwNjc1aHR321BaBaaXRlc3QvbWVkaWEvL2ltYWdlcy9ia2cvYmtnX3doaXRlLnBuZw%3D%3D">/bkg_white.png</a><br />
<a href="http://www.example.com/index.php?goto=L2Rh%123BB%bi123abBAjMyMC91c2dyLzMwMzAwNjc1aHR321BaBaaXRlc3QvbWVkaWEvL2ltYWdlcw%3D%3D">/images</a><a href="http://www.example.com/index.php?goto=L2Rh%123BB%bi123abBAjMyMC91c2dyLzMwMzAwNjc1aHR321BaBaaXRlc3QvbWVkaWEvL2ltYWdlcy9ia2c%3D">/bkg</a><a href="http://www.example.com/index.php?goto=L2Rh%123BB%bi123abBAjMyMC91c2dyLzMwMzAwNjc1aHR321BaBaaXRlc3QvbWVkaWEvL2ltYWdlcy9ia2cvcm93X2dyYWQwMS5wbmc%3D">/row_grad01.png</a><br />
如果您点击第3或第4个链接中的bkg
,则会显示:
/bkg_white.png
/row_grad01.png
单击其中一个将在浏览器中打开该文件。无论如何,你可能对其中的一些,所有这些,或者都没有兴趣!很难猜到你现在拥有的东西和你想要达到的目标。