2017-04-05 46 views
1

我想創建一個畫廊,使用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>標籤的文件夾名稱,並追加到URL,這樣它會顯示它的子文件夾中。

+0

可讀性明智:除非沒有其他選項(類更酷),否則不要使用'style'。使用'foreach'而不是'for'循環。變量名稱可能更具描述性,「albflcnt」沒有多大意義。 –

回答

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); 

?> 
+0

嗨對不起,我忘了提及,我需要讓他們點擊,我想要檢索的價值是我點擊 –

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 /> 

如果你點擊bkg在第三或第4個鏈接,它將顯示:

/bkg_white.png 
/row_grad01.png 

單擊其中一個將在瀏覽器中打開該文件。無論如何,你可能對這一切都感興趣,或者全都是這樣!很難猜出你看到你現在擁有什麼,以及你試圖達到什麼目標。