我正在创建一个包含两个组件的网页
我的文件夹结构如下:
目录 - 资源
子目录 - > location1,location2,location3
图像 - 在每个位置我都有 - > image1,image2,image3
列出所有文件夹和打印所有图像的PHP代码工作正常。但是我想要的功能,当用户从第一个php中选择一个文件夹时,我希望通过第二个php呈现图像。
directories.php
PHP代码列出文件夹(这很好):
<?php
$direc = "";
if($handle = opendir('resources/')){
while (false !== ($file = readdir($handle)))
{
if (($file != ".") && ($file != ".."))
{
$direc .= '<li name="fold"><a href="#">'.$file.'</a></li>';
$nam = $_POST['fold'];
echo $nam; //This is not echoing
}
}
closedir($handle);
}
?>
<form method="POST" action="images.php">
<ul>
<?php echo $direc; ?>
</ul>
</form>
images.php
PHP CODE列出所有图像:此代码也可以正常工作
<div>
<?php
$files = glob("resources/location1/*.jpg");
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i];
echo '<img src="'.$num.'" height = "200" width = "180" id="thumbNails"/>'."<br /><br />";
$filname = basename($num, ".jpg");
$filnam = substr($filname, -5);
print $filnam."<br />";
}
?>
</div>
不知道如何在第二个PHP中适应这一行:
$files = glob($nam/"*.jpg");
您可能想要使用Recursive
,这样更容易。试试这个:
整个代码
$root = __DIR__;
$selected_folder = $_POST['fold'];
$iter = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($root, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
);
$folder_paths = array($root);
$image_paths = array();
foreach ($iter as $path => $dir) {
if ($dir->isDir()) {
$folder_paths[] = $path;
if(!empty(glob($path .'/*.jpg'))){
$image_paths[] = glob($path .'/*.jpg');
}
}
}
$display_images = array();
foreach($image_paths as $folders){
foreach($folders as $folder){
if(strpos($folder, $selected_folder) !== false){
$display_images[] = $path;
}
}
}
print_r($display_images);
好的,我解决了这个问题。以下是代码:
directories.php
<html>
<head>
<title></title>
</head>
<body>
<div>
<form action="test1.php" method="get">
<select id="dirs" name="myname">
<option value="" selected="selected">SELECT A PROJECT</option>
<?php
$dirs = glob("/var/www/html/yourdirectory/*", GLOB_ONLYDIR);
foreach($dirs as $val){
echo '<option value="'.$val.'">'.basename($val)."</option>\n";
}
?>
</select>
<input type="submit" name="submit" value="click">
</form>
</div>
</body>
</html>
subdirectories.php
<html>
<head>
<title></title>
</head>
<body>
<?php
$var = $_GET['myname'];
$direc = "";
if($handle = opendir($var)){
while (false !== ($file = readdir($handle)))
{
if (($file != ".") && ($file != ".."))
{
$direc .= '<li><a href="'.$file.'">'.$file.'</a></li>';
}
}
closedir($handle);
}
?>
<div>
<h5>LIST OF DIRECTORIES</h5>
<ul>
<form action="footer.php" method="post">
<p name="filesel"><?php echo $direc ?></p>
</form>
</ul>
</div>
</body>
</html>