开发者

PHP echo file contents

I have a pdf file which is located off my webpage's root. I want to serve a file in ../cvs to my users using php.

Here is the code I have sofar:

header('Content-type: application/pdf');

$file = file_get_contents('开发者_开发技巧/home/eamorr/sites/eios.com/www/cvs/'.$cv);
echo $file;

But when I call this php page, nothing gets printed! I'd like to simply serve the pdf file stored whose name is in $cv (e.g. $cv = 'xyz.pdf').

The ajax response to this PHP page returns the text of the pdf (gobbldy-gook!), but I want the file, not the gobbldy-gook!

I hope this makes sense.

Many thanks in advance,


Here's the AJAX I'm using

$('#getCurrentCV').click(function(){
    var params={
        type: "POST",
        url: "./ajax/getCV.php",
        data: "",
        success: function(msg){
            //msg is gobbldy-gook!
        },
        error: function(){

        }
    };
    var result=$.ajax(params).responseText;
});

I'd like the user to be prompted to download the file.


Don't use XHR (Ajax), just link to a script like the one below. The HTTP headers the script outputs will instruct the browser to download the file, so the user will not navigate away from the current page.

<?php
// "sendfile.php"

//remove after testing - in particular, I'm concerned that our file is too large, and there's a memory_limit error happening that you're not seeing messages about.
error_reporting(E_ALL);
ini_set('display_errors',1);

$file = '/home/eamorr/sites/eios.com/www/cvs/'.$cv;

//check sanity and give meaning error messages
// (also, handle errors more gracefully here, you don't want to emit details about your
//  filesystem in production code)
if (! file_exists($file)) die("$file does not exist!");
if (! is_readable($file)) die("$file is unreadable!");

//dump the file
header('Cache-Control: public'); 
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="some-file.pdf"');
header('Content-Length: '.filesize($file));

readfile($file);

?>

Then, simplify your javascript:

$('#getCurrentCV').click(function(){
     document.location.href="sendfile.php";
});


How about using readfile instead? Provided that the file exists, that should work. Make sure your web process has permission to read the directory and the file. There is an example on the readfile page that sets some headers as well.


I'm trying to prompt the user to download the pdf file.

You can't (and don't need to) send a binary download to the user's browser using Ajax. You need to send the user to an actual URL where the PDF is located.

Use @timdev's code, and point the user there using e.g.

location.href = "scriptname.php";


It sounds like you're trying to serve the pdf to user for download via AJAX.

What you want to do is use AJAX to confirm the files exists, and security if any, then simply use js to redirect the browser to that files url, or in this case the url of the php script delivering the pdf. When your browser gets the pdf header it wont try to redirect the page itself but prompt for download, or whatever the users browser settings are.

Something like: (js)

window.location.href = http://example.com/getApdf.php?which=xyz

(php)

if( !isset( $_GET['which'] ) ) die( 'no file specified' );
if( !file_exists( $_GET['which'] . '.pdf' ) ) die( 'file doesnt exist');

header('Content-type: application/pdf');

readfile( $_GET['which'] . '.pdf' );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜