如何在PHP中通过单击按钮浏览文件并显示其内容?

如何在PHP中通过单击按钮浏览文件并显示其内容?,php,arrays,html,css,laravel-5.3,Php,Arrays,Html,Css,Laravel 5.3,如何通过单击Next按钮和Previous按钮浏览文件夹中的文件,然后在表格单元格中显示文件内容 这是我的示例输出,但遗憾的是它对我不起作用: <!DOCTYPE html> <html> <head> <title>Browse Files</title> </head> <body> <?php $dir = '/xampp/htdocs/files'; $file = array_diff(scand

如何通过单击
Next
按钮和
Previous
按钮浏览文件夹中的文件,然后在表格单元格中显示文件内容

这是我的示例输出,但遗憾的是它对我不起作用:

<!DOCTYPE html>
<html>
<head>
<title>Browse Files</title>
</head>
<body>
<?php
$dir = '/xampp/htdocs/files';
$file = array_diff(scandir($dir), array('.','..'));

function showContent($fdata) {
    $fopen = fopen($fdata,'r') or die ("Could not open file");
    $content = fread($fopen,filesize($fdata)) or die("Could not read the file");
    echo $content;
    fclose($fopen);
}
  ?>
<table border = "1" cellpadding = "10" align = "center" width = "500">
    <frame method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
        <tr>
            <td>
                <h3>File Contents: </h3> <br />
                <?php
                    if (isset($_POST['next'])) {
                        for($i = 0; $i < sizeof($file); $i++) {
                            $path = $dir ."/". $file[$i];
                            showContent($path);
                        }   
                    }

                    if (isset($_POST['previous'])) {
                        for($x = sizeof($file); $x > sizeof($file); $i--) {
                            $path = $dir ."/". $file[$i];
                            showContent($path);
                        }   
                    }
                ?>      
            </td>
        </tr>
            <td align = "center">
                <input type = "submit" name = "previous" value = "Previous">
                <input type = "submit" name = "next" value = "Next">
            </td>
    </frame>
</table>
</body>
</html>

以下是我使用的代码:

<!DOCTYPE html>
<html>
<head>
<title>Browse Files</title>
</head>
<body>
<?php
$dir = '/xampp/htdocs/files';
$file = array_diff(scandir($dir), array('.','..'));

function showContent($fdata) {
    $fopen = fopen($fdata,'r') or die ("Could not open file");
    $content = fread($fopen,filesize($fdata)) or die("Could not read the file");
    echo $content;
    fclose($fopen);
}
  ?>
<table border = "1" cellpadding = "10" align = "center" width = "500">
    <frame method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
        <tr>
            <td>
                <h3>File Contents: </h3> <br />
                <?php
                    if (isset($_POST['next'])) {
                        for($i = 0; $i < sizeof($file); $i++) {
                            $path = $dir ."/". $file[$i];
                            showContent($path);
                        }   
                    }

                    if (isset($_POST['previous'])) {
                        for($x = sizeof($file); $x > sizeof($file); $i--) {
                            $path = $dir ."/". $file[$i];
                            showContent($path);
                        }   
                    }
                ?>      
            </td>
        </tr>
            <td align = "center">
                <input type = "submit" name = "previous" value = "Previous">
                <input type = "submit" name = "next" value = "Next">
            </td>
    </frame>
</table>
</body>
</html>

浏览文件

除了首先获取文件夹的内容外,我真的想不出其他方法:

# You want to feed in the base directory, the current file path, and whether
# to advance or previous
function getContents($dir,$current = false,$advance = false)
{
    # You don't want to get the file contents of images, it's garbled
    $img        =   ['gif','jpg','jpeg','png'];
    # Get the contents of the folder and remove directories and dots
    $contents   =   array_values(array_filter(scandir($dir),function($v) use ($dir){
        if(in_array($v,['.','..']))
            return false;

        if(is_dir($dir.'/'.$v))
            return false;

        return true;
    }));
    # If no current file, grab the first one
    if(empty($current)) {
        if(!empty($contents)) {
            $nPath  =   array_shift($contents);
            $next   =   $dir.'/'.$nPath;                
            return [
                'img' => (is_file($next) && in_array(strtolower(pathinfo($next,PATHINFO_EXTENSION)),$img)),
                'file' => (is_file($next))? basename($next) : false,
                'current'=> (!empty($nPath))? $next : false,
                'data'=>(!empty($nPath) && is_file($next))? file_get_contents($next) : false
            ];
        }
    }
    else {
        # If there is a current file being viewed..
        foreach($contents as $key => $value) {
            # See if this file is the file being viewed
            if($dir.'/'.$value == $current) {
                # If want to see next, get the next key
                if($advance == 'next')
                    $nPath  =   (!empty($contents[$key+1]))? $contents[$key+1] : $contents[0];
                # Rewind the current array
                else {
                    prev($contents);
                    prev($contents);
                    $nPath  =   (!empty($contents[key($contents)]))? $contents[key($contents)] : $contents[$key];
                }
                $next   =   $dir.'/'.$nPath;                
                return [
                    'img' => (is_file($next) && in_array(strtolower(pathinfo($next,PATHINFO_EXTENSION)),$img)),
                    'file' => (is_file($next))? basename($next) : false,
                    'current'=>$next,
                    'data'=>(!empty($nPath) && is_file($next))? file_get_contents($next) : false
                ];
            }
        }
    }
}
# Set the action
$action     =   (!empty($_POST['action']))? strtolower($_POST['action']) : false;
# Capture current path
$curr       =   (!empty($_POST['current']))? $_POST['current'] : false;
# Retrieve the next or prev
$contents   =   getContents(__DIR__,$curr,$action);
?>
<table border = "1" cellpadding = "10" align = "center" width = "500">
        <tr>
            <td>
                <h3>File Contents: <?php echo $contents['file'] ?></h3> <br />
                <?php if(!empty($contents['img'])): ?>
                <img src="<?php echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$contents['current']) ?>" style="max-width: 100px;" />
                <?php else: ?>
                <pre><?php echo htmlspecialchars($contents['data']) ?></pre>
                <?php endif ?>
            </td>
        </tr>
            <td align = "center">
                <form method="POST" action="">
                    <input type="hidden" name="current" value="<?php echo $contents['current'] ?>" />
                    <input type = "submit" name="action" value = "Previous">
                    <input type = "submit" name="action" value = "Next">
                </form>
            </td>
    </frame>
</table>
</body>
</html>
#是否要输入基本目录、当前文件路径以及
#前进
函数getContents($dir,$current=false,$advance=false)
{
#您不想获取图像的文件内容,它是乱码的
$img=['gif'、'jpg'、'jpeg'、'png'];
#获取文件夹的内容并删除目录和点
$contents=array\u值(array\u过滤器(scandir($dir),函数($v)use($dir){
if(在数组中($v,['.','..]]))
返回false;
if(is_dir($dir./'.$v))
返回false;
返回true;
}));
#如果没有当前文件,请抓取第一个文件
如果(空($当前)){
如果(!空($contents)){
$nPath=数组移位($contents);
$next=$dir.'/'.$nPath;
返回[
'img'=>(is_文件($next)和&in_数组(strtolower(路径信息($next,路径信息扩展)),$img)),
'file'=>(is_file($next))?basename($next):false,
“当前”=>(!empty($nPath))?$next:false,
'data'=>(!empty($nPath)和&is_file($next))?file_get_contents($next):false
];
}
}
否则{
#如果正在查看当前文件。。
foreach($key=>$value形式的内容){
#查看此文件是否为正在查看的文件
如果($dir.'/'。$value==$current){
#如果要查看下一个,请获取下一个键
如果($advance=='next')
$nPath=(!empty($contents[$key+1])?$contents[$key+1]:$contents[0];
#倒带当前阵列
否则{
prev($目录);
prev($目录);
$nPath=(!empty($contents[key($contents)])?$contents[key($contents)]:$contents[$key];
}
$next=$dir.'/'.$nPath;
返回[
'img'=>(is_文件($next)和&in_数组(strtolower(路径信息($next,路径信息扩展)),$img)),
'file'=>(is_file($next))?basename($next):false,
“当前”=>下一个$,
'data'=>(!empty($nPath)和&is_file($next))?file_get_contents($next):false
];
}
}
}
}
#采取行动
$action=(!empty($_POST['action']))?strtolower($_POST['action']):false;
#捕获当前路径
$curr=(!空($_POST['current']))$_POST['current']:false;
#检索下一个或上一个
$contents=getContents(目录,$curr,$action);
?>
文件内容:
“style=”最大宽度:100px;" />
非常感谢您的回复,我非常感谢,顺便说一句,新年快乐:)我尝试了上面的代码,但当我单击上一个按钮时遇到了问题-当我单击它时,表格单元格为空,或者我收到了一个“未知错误”“。对我来说,有一个地方需要更多的故障排除。它工作正常,但当它到达目录的末尾时,就出现了问题。”。我能做下一个或上一个,除非它击中那个点。我可能会对它进行故障排除,但我所做的应该是大部分工作。:P呵呵,我理解并感谢您的帮助:)我稍后会进行试验,也许这是一个个人问题,但与上面的代码有关,我在哪里可以学习这一部分,因为老实说,我只是一个新手:
$contents=array\u values(array\u filter(scandir($dir),函数($v)使用($dir){if(在数组($v,['.','..'])返回false;if(is_dir($dir.'/'.$v))返回false;返回true;})我更新了应该删除未定义索引的一行。现在唯一的问题是,当您到达目录内容的末尾时,它仍然会变为空白,但这只是另一行需要修改才能修复的内容。你想清楚哪一部分?还有这一部分:
return['img'=>(is_file($next)&&in_数组(strtolower(pathinfo($next,pathinfo_扩展名)),$img)),'file'=>(is_file($next))?basename($next):false,'current'=>$next,'data'=>(!empty($nPath)和is_file($next))?文件获取内容($next):false];