【问题标题】:How to browse files and show its content by clicking buttons in PHP?如何通过单击 PHP 中的按钮来浏览文件并显示其内容?
【发布时间】:2018-06-10 09:40:55
【问题描述】:

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

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

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>

【问题讨论】:

    标签: php arrays html css laravel-5.3


    【解决方案1】:

    除了先获取文件夹的内容之外,我真的想不出任何办法:

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

    您可能需要稍微调整一下,但它已经非常接近了……不过我不知道它的效率如何。

    【讨论】:

    • 非常感谢您的回复,非常感谢,顺便说一句,新年快乐 :) 我在上面尝试了您的代码,但是当我单击“上一个”按钮时遇到问题 - 当我单击它时表格单元格为空白或我收到“未识别错误”。
    • 是的,对我来说,有一个地方需要更多的故障排除。它有效,但是当它到达目录的末尾时出现了问题。我可以做下一个或上一个,除非它到达那个位置。我可能可以解决它,但我所拥有的应该是大部分工作。
    • :呵呵,我明白了,谢谢你的帮助:) 稍后我会尝试一下,也许这是个人问题但与上面的代码有关,我在哪里可以学习这部分因为老实说我只是一个新手: $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; }));
    • 我更新了应该删除未定义索引的一行。现在唯一的问题是,当您到达目录内容的末尾时,它仍然会变为空白,但这只是另一行必须修改才能解决的问题。你想清楚哪一部分?
    • 还有这个: return [ 'img' =&gt; (is_file($next) &amp;&amp; in_array(strtolower(pathinfo($next,PATHINFO_EXTENSION)),$img)), 'file' =&gt; (is_file($next))? basename($next) : false, 'current'=&gt;$next, 'data'=&gt;(!empty($nPath) &amp;&amp; is_file($next))? file_get_contents($next) : false ];
    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    相关资源
    最近更新 更多