Using php's ability to call upon the browser to display a given PDF it doesn't work on my web host. But it works on my local xamp server with apache.
PHP:
$title = $_GET['title'];
$loc = $_GET['loc'];
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$title);
#readfile($loc);
Expected Output: Supposed to be the PDF document being rendered like it does on my local web server.
Actual but unwanted output:
%PDF-1.6 %���� 1638 0 obj <> endobj xref 1638 44 0000000016 00000 n ...
Can be seen here:
http://neilau.me/view.php?title=business-studies-hsc-notes-2006.pdf&loc=pdf/criteria/business-studies/2006/business-studies-hsc-notes-2006.pdf
Is this fixed through changing something on my cpanel? As my code isn't faulty... It works on my local web server.
Use this code
<?php
$file = 'dummy.pdf';
$filename = 'dummy.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
?>
You need to make sure you are not sending any text before writing headers.
Example of what not to do:
<!DOCTYPE html>
<html>
...
<?php
header("Content-type: application/pdf");
Example of how to fix that:
<?php
header("Content-type: application/pdf");
?>
<!DOCTYPE html>
<html>
...
In addition your script is very insecure. Here's what you should do, your entire PHP script should be:
<?php
$loc = filter_input(INPUT_GET,"loc");
$title = filter_input(INPUT_GET,'title')?:basename($loc);
if (!is_readable($loc)) {
http_response_code(404);
echo "Cannot find that file";
die();
}
if (strtolower(pathInfo($loc,PATHINFO_EXTENSION)) != "pdf") {
http_response_code(403);
echo "You are not allowed access to that file";
die();
}
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$title);
header("Content-Length: ".filesize($loc));
readfile($loc);
If you want to show things like a page title or a frame around it, you should use an iframe in another "wrapper" page:
<?php
$loc = filter_input(INPUT_GET,"loc");
$title = filter_input(INPUT_GET,'title')?:basename($loc);
?>
<html><head><title>My title</title></head>
<body>
<iframe src="/view.php?<?php echo ($loc?"loc=$loc":"").($title?"title=$title":"") ?>">
</iframe>
</body>
<html>
Related
I have the following PHP code:
function download_file($file, $description=null, $filename=false){
$filename = $filename || basename($file);
ob_start();
if(is_string($description)){
$description = preg_replace("/\$1/", $filename, $description);
echo $description;
}
sleep(2);
header('Content-Type: '.$this->file_mimetype($file));
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file) . "\"");
readfile($file);
header("Content-Type: text/html");
ob_end_flush();
}
download_file("https://raw.githubusercontent.com/Gethis/ED/master/easydevop.class.php", "Downloading easydevop.class.php");
The problem is that it is not echoing "Downloading easydevop.class.php" before the download. I also tried echoing it after all the headers, but that didn't work either. Please, any help?
As you can see I did use ob_start() and ob_end_flush()
You can't use "echo" (showing HTML-content) and send file to user at the same time.
You can show HTML-page first, and then redirect user to the file, using
HTML-redirect
<META HTTP-EQUIV="REFRESH" CONTENT="0;URL=http://url.to/file/or_script/that_send_file/">
or javascript redirect How do I redirect with Javascript?
As I already mentioned you cannot display echo when downloading file. When you download file you can just download file, nothing more.
However using JavaScript you can display message before starting downloading. Here is the test script:
<?php
if (isset($_GET['id'])) {
$file = 'testfile.txt';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<script type="text/javascript">
function showDownload(message) {
document.getElementById("hidden").innerHTML = message;
document.getElementById("link1").style.display = 'none'; // you can even hide download link if you want
}
</script>
<div id="hidden"></div>
Download
</body>
</html>
I am trying to create link to save browser output as file without creating file on server.
This is what I got so far:
<?php
ob_start();
?>
<html>
webpage content
</html>
<?php
$page = ob_get_contents();
ob_end_flush();
$file= time().'.html';
file_put_contents($file, $page);
ob_start();
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
ob_end_flush();
?>
Download output as file
How can i create such link WITHOUT SAVING file on server?
Thank you for your suggestions/ideas/code.
Why that complicated? Do it straight forward instead:
<?php
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.time().'.html"');
?>
<html>
webpage content
</html>
You have two fairly easy options (there are others, but they would be more complicated) :
Option 1, use a data url:
$pageData = base64_encode($page);
$finfo = new finfo(FILEINFO_MIME);
$pageDataMime = $finfo->buffer($page);
$pageDataURL = 'data:' . $pageDataMime . ';base64,'.$pageData;
?>
Download output as file
Option 2, use query string to determine if output should be downloaded or not:
if($_GET['download_data']) {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
echo $page;
exit();
} else {
// Output HTML as normal, including:
Download output as file
}
Open a file handle like this fopen('php://output', 'w'); and output to it.
Just echo $page:
<?php
ob_start();
?>
<html>
webpage content
</html>
<?php
$page = ob_get_contents();
ob_end_flush();
$file= time().'.html';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . strlen($page));
echo $page;
?>
Is it possible to display the content of a php file to another php file?
I have here an example where in the PDF is displayed in a php file.
<?php
$file = 'path of your PDF file';
$filename = 'custom pdf file name'; /* Note: Always use .pdf at the end. */
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
?>
but when I tried to display a php file to another php file. Its not working.
<?php
$file = 'path to my php file';
$filename = 'custom pphp file name';
header('Content-type: text/html');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
?>
Can anyone tell me what I'm doing wrong?
Here is one way to display the contents of another .php file:
show_included_file.php
<?php
include ('file1.php');
?>
file1.php
<?php
echo "This is the included file.";
?>
when upon entering show_included_file.php in your web browser,
will echo This is the included file if that is the intented result.
From a form input
Another way of showing content taken from a form (POST method) variable is this:
From a form input, for example <input type="text" name="variable_1">
Now if the user enters Hello world in the field,
then in your handler $variable_1 = $_POST['variable_1'];
you could then do echo $variable_1; and it would echo Hello world
PHP handler
<?php
$variable_1 = $_POST['variable_1'];`
echo $variable_1;
?>
I hope this is what you are looking for.
<?php
echo htmlentities(file_get_contents("yourphpfile.php"));
I am trying to write a script in Yii for downloading files from the server.
The files are located in webroot of the Yii project,
but I got every time file not exist error, could anyone see where is wrong:
public function actionDownload($id) {
$audio = Audio::model()->findByPk($id);
$file = Yii::getPathOfAlias('webroot') . $audio->path;
if (file_exists($file)) {
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $audio->path);
header('Content-Length: ' . filesize($audio->path));
$audio->downloaded = $audio->downloaded + 1;
$audio->save();
}else{
echo "file not exist: ".$file;
}
exit;
}
error I got is:
file not exist: /var/www/vhosts/ikhwanbiz.org/httpdocs/userfiles/reklames/media/deneme/sen%20dep%20olmisem.mp3
Thanks
Regards
Bili
Bili, this works well for me and seem to be fine on most browsers.
$filename = 'your_file_name.csv';
header('Content-Disposition: attachment; charset=UTF-8; filename="'.$filename.'"');
$utf8_content = mb_convert_encoding($content, "SJIS", "UTF-8");
echo $utf8_content;
Yii::app()->end();
return;
Hope it helps, good luck!
It looks like the filename portion $audio->path is URL-encoded, while the name of the actual file on the server is not. You should fix it at the source (no idea where that path is set from), but in the meantime an easy workaround would be to write
$file = Yii::getPathOfAlias('webroot') . urldecode($audio->path);
This is more of a php question than a yii one.
for eg,
<?php
header("Content-disposition: attachment; filename=huge_document.pdf");
header("Content-type: application/pdf");
readfile("huge_document.pdf");
?>
source: http://webdesign.about.com/od/php/ht/force_download.htm
I'm looking to add a "Download this File" function below every video on one of my sites. I need to force the user to download the file, instead of just linking to it, since that begins playing a file in the browser sometimes. The problem is, the video files are stored on a separate server.
Any way I can force the download in PHP?
You could try something like this:
<?php
// Locate.
$file_name = 'file.avi';
$file_url = 'http://www.myremoteserver.com/' . $file_name;
// Configure.
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
// Actual download.
readfile($file_url);
// Finally, just to be sure that remaining script does not output anything.
exit;
I just tested it and it works for me.
Please note that for readfile to be able to read a remote url, you need to have your fopen_wrappers enabled.
Tested download.php file is
function _Download($f_location, $f_name){
$file = uniqid() . '.pdf';
file_put_contents($file,file_get_contents($f_location));
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename=' . basename($f_name));
readfile($file);
}
_Download($_GET['file'], "file.pdf");
and the link to download is
Descargar
Try this:
<?php
$FileName = '/var/ww/file.txt';
header('Content-disposition: attachment; filename="'.$FileName.'"');
readfile($FileName);
The key is the header(). You need to send the header along with the download and it will force the "Save File" dialog in the user's browser.
<?php
$file_name = 'video.flv';
$file_url = 'http://www.myserver.com/secretfilename.flv';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
echo file_get_contents($file_url);
die;
?>
I don't know this is the best way or not but I like that, short & simple.
If you want to download file when you visit URL, you can do like below
<a href="resume.pdf" download></a>
<script>document.querySelector('a').click();</script>
index.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>download</title>
</head>
<body>
download
</body>
</html>
.htaccess
Options -Indexes
Options +FollowSymlinks
deny from all
download.php
$file = "pdf/teste.pdf";
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit();
}
<?php
$FileName = '/var/ww/file.txt';
header('Content-disposition: attachment; filename="'.$FileName.'"');
readfile($FileName);
using this code. is it possible to save the file name to what you want. for example you have url: http://remoteserver.com/file.mp3 instead of "file.mp3" can you use this script to download the file as "newname.mp3"