PHP echo file contents -
i have pdf file located off webpage's root. want serve file in ../cvs
users using php.
here code have sofar:
header('content-type: application/pdf'); $file = file_get_contents('/home/eamorr/sites/eios.com/www/cvs/'.$cv); echo $file;
but when call php page, nothing gets printed! i'd serve pdf file stored name in $cv
(e.g. $cv = 'xyz.pdf'
).
the ajax response php page returns text of pdf (gobbldy-gook!), want file, not gobbldy-gook!
i hope makes sense.
many in advance,
here's ajax i'm using
$('#getcurrentcv').click(function(){ var params={ type: "post", url: "./ajax/getcv.php", data: "", success: function(msg){ //msg gobbldy-gook! }, error: function(){ } }; var result=$.ajax(params).responsetext; });
i'd user prompted download file.
don't use xhr (ajax), link script 1 below. http headers script outputs instruct browser download file, user not navigate away current page.
<?php // "sendfile.php" //remove after testing - in particular, i'm concerned our file large, , there's memory_limit error happening 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 , give meaning error messages // (also, handle errors more gracefully here, don't want emit details // filesystem in production code) if (! file_exists($file)) die("$file not exist!"); if (! is_readable($file)) die("$file unreadable!"); //dump 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 javascript:
$('#getcurrentcv').click(function(){ document.location.href="sendfile.php"; });
Comments
Post a Comment