PHP,在浏览器中显示pdf文件

时间:2014-01-19 08:25:51

标签: php pdf

我真的想在broswer中读取pdf文件,我一直在网上搜索,我尝试了很多代码,这是我的代码:

<?php

$directory = 'C:/Users/Oscar/Documents/';

if(isset($_POST['libros']))
{

    $dir = $directory.'libros/';

    file_put_contents('cache', $dir);

    $files = array_diff(scandir($dir), array('..', '.'));

    ob_clean();
    ob_start();
    foreach($files as $file):?>


    <table border="2px">
        <tr>
            <td>
                <form method="post">
                    <input type="submit" value="<?php echo $file; ?>" name="libros-file" /> 
                </form>
            </td>
        </tr>
    </table>

    <?php endforeach;;

}

if(isset($_POST['musica']))
    echo "musica exists!";

if(isset($_POST['test']))
{
    $dir = $directory.'test/';

    file_put_contents('cache', $dir);

    $files = array_diff(scandir($dir), array('..', '.'));


    foreach($files as $file):?>

    <table border="2px">
        <tr>
            <td>
                <form method="post">
                    <input type="submit" value="<?php echo $file; ?>" name="test-file" />   
                </form>
            </td>
        </tr>
    </table>

    <?php endforeach;


}

if(isset($_POST['test-file']))
{   
    //print_r($_POST['file']);
    $dir = file_get_contents('cache');
    unlink('cache');
    $file = file_get_contents($dir.$_POST['test-file']);
    echo $file;

}

if(isset($_POST['libros-file']))
{
    ob_clean();
    $dir = file_get_contents('cache');
    unlink('cache');
    $file = file_get_contents($dir.$_POST['libros-file']);


    header('Pragma: public');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Content-Type: application/pdf');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($file));
    @read($file);

}

我试图读取一个在libros中的文件,但是所有的时间,即使我尝试了所有代码,我也会看到pdf查看器,但我什么都看不见,我也得到一个建议,说pdf可能没有正确显示。请帮忙

0 个答案:

没有答案