我要感谢任何能够以任何可能的方式帮助我的人,我对php/编码非常陌生,所以我甚至不确定我是否走上了正确的道路。
我想知道是否可以只使用php和html创建两步或动态下拉菜单,用目录中的文件夹填充第一个下拉菜单。
到目前为止,我有
<?php
// Set the path of the dir you want displayed...
$path="./track";
$handle=opendir($path);
while ($file=readdir($handle))
{
echo "'t<option value='".$file."'>".$file."</option>'n";
}
?>
这让我可以从目录中获取文件夹,我如何继续创建第二个下拉菜单,让你从第一个下拉菜单中选择的任何文件夹中选择任何文件,然后在网站上显示?
我正在努力实现的目标示例如下:http://postimg.org/image/im03tjh0d/
<?php
/**
* Check if we have submit the folder
* Check if we have submit the file
*/
$post_folder = isset($_POST['folder']) ? $_POST['folder']: null;
$post_file = isset($_POST['file_name']) ? $_POST['file_name'] : null;
/**
* Built out the Folder submission form
*/
// Set the path of the dir you want displayed...
$path="./";
echo '<form method="POST" action="', $_SERVER['PHP_SELF'],'">';
echo '<select name="folder">';
foreach (new DirectoryIterator($path) as $asset){
if($asset->isDot()) continue;
if($asset->isDir()){
/**
* If we've posted which folder we want to view the contents of
* then automatically make that selected in the dropdown on page load
*/
$selected = (isset($post_folder) && $post_folder == $asset->getFileName()) ? 'selected' : '';
echo "'t<option value='".$asset->getFilename()."' $selected>".$asset->getFilename()."</option>'n";
}
}
echo '</select>';
if(isset($post_folder)){
echo '<select name="file_name">';
$folder_to_iterate = $path.$post_folder;
foreach (new DirectoryIterator($folder_to_iterate) as $asset){
if($asset->isFile()){
/**
* If we've posted which folder we want to view the contents of
* then automatically make that selected in the dropdown on page load
*/
$selected = (isset($post_file) && $post_file == $asset->getFileName()) ? 'selected' : '';
echo "'t<option value='".$asset->getFilename()."' $selected>".$asset->getFilename()."</option>'n";
}
}
echo '</select>';
}
$btn_text = isset($post_folder) ? 'Show File Contents' : 'Show Folder Contents';
echo '<input type="submit" value="', $btn_text ,'"/>';
echo '</form>';
if(isset($post_file)){
$file_to_read = $path.$post_folder.'/'.$post_file;
echo '<pre>';
echo htmlentities(file_get_contents($file_to_read));
echo '</pre>';
}
使用ajax和文件系统函数。