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

How to browse files and show its content by clicking buttons in PHP?

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

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

Image Output

这是我使用的代码:

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

您可能需要稍微改进一下,但它非常接近...不过我不知道它的效率如何。