Select 显示文本文件 php html
Select to display text file php html
这里是 100% 的新手。 :)
一直在搜索 google 但我找不到任何完整的解决方案。
所以我想做的是显示文件名行,例如
文本 1
文字2
文本3
....
单击时显示它的文本内容。
到目前为止,我可以从我想要的目录中获取文件列表:
$dir_path = "files$row[CardID]/";
foreach(glob($dir_path.'*') as $file) {
$nname = substr($file, strpos($file,"/")+1);
echo $nname."<br>"; /* this returns me text1 text2 text3 */
};
所以我的问题是如何让它在单击时显示文本内容(弹出窗口或 html 页面或......我不知道)?
非常感谢您的帮助
你可以这样做:
if(!isset($_GET['file'])) {
die('Please input a file');
}
$file = stripslashes($_GET['file']);
echo file_get_contents($_GET['file']);
或者只包含一个目录:
echo '<ul>';
foreach (glob("txt_include/*.txt") as $filename)
{
echo '<li><a href="?file=' . $filename . '">' . $filename . '</a></li>';
}
echo '</ul>';
使用数据库。示例(还没有完成,抱歉,我还不是专业人士)
$conn = mysqli_connect('HOST', 'USER', 'PASS', 'DATABASE');
$sql = 'SELECT * FROM topic ORDER BY `id` DESC';
$rqt = mysqli_query($conn, $sql);
while($data = mysql_fetch_array($rqt))
$text1 = $data['text']
echo $text1;
这里是 100% 的新手。 :) 一直在搜索 google 但我找不到任何完整的解决方案。 所以我想做的是显示文件名行,例如
文本 1
文字2
文本3 .... 单击时显示它的文本内容。
到目前为止,我可以从我想要的目录中获取文件列表:
$dir_path = "files$row[CardID]/";
foreach(glob($dir_path.'*') as $file) {
$nname = substr($file, strpos($file,"/")+1);
echo $nname."<br>"; /* this returns me text1 text2 text3 */
};
所以我的问题是如何让它在单击时显示文本内容(弹出窗口或 html 页面或......我不知道)? 非常感谢您的帮助
你可以这样做:
if(!isset($_GET['file'])) {
die('Please input a file');
}
$file = stripslashes($_GET['file']);
echo file_get_contents($_GET['file']);
或者只包含一个目录:
echo '<ul>';
foreach (glob("txt_include/*.txt") as $filename)
{
echo '<li><a href="?file=' . $filename . '">' . $filename . '</a></li>';
}
echo '</ul>';
使用数据库。示例(还没有完成,抱歉,我还不是专业人士)
$conn = mysqli_connect('HOST', 'USER', 'PASS', 'DATABASE');
$sql = 'SELECT * FROM topic ORDER BY `id` DESC';
$rqt = mysqli_query($conn, $sql);
while($data = mysql_fetch_array($rqt))
$text1 = $data['text']
echo $text1;