I want to open the pdf file on my browser, but the file won't open. Chrome said "Failed to load PDF document". Please help.
Here's my code:
<?
include 'connection.php';
$query=mysql_query("SELECT * FROM book");
$p=mysql_fetch_array($query);
$ebook=$p['ebook'];
$title=$p['title'];
$file = "admin/upload/$ebook";
$filename = "$title";
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
#readfile($file);
?>
Any help is appreciated!
While I am not sure if this approach is applicable to your situation, if you are running jQuery this will accomplish what you are trying to do according to your question:
No db query is required... you can use glob('admin/upload/*')
PHP code:
echo '<select id="getPDF" name="getPDF">';
echo '<label>Select PDF</label>';
echo '<option value=""> Select PDF</option>';
foreach(glob('admin/upload/*') as $specific_pdf) {
$this_pdf = str_replace('.pdf','',basename($specific_pdf));
$this_pdf = ucwords(str_replace('-',' ',$this_content));
$this_pdf_url = $specific_pdf; # ADJUST THIS TO FULL URL TO PDF
echo '<option value="'.$this_pdf_url.'">'.$this_pdf.'</option>';
}
echo '</select>';
jQuery:
$(function() {
$( "#getPDF" ).change(function(){
var pdf_url = $(this).val();
window.open(pdf_url);
});
});
This will open the pdf in a new window/tab.
Related
I am new to PHP and trying to implement below functionality through code give
Functionality:
I have one Download button and upon clicking i should be able to download my .pdf file stored under htdocs-->xxfilename-->abc.pdf;
Code:
I am using below code in my xyz.php web page
<?php
$title = "Learning";
$content = '
<h3> Intro</h3>
<p>
Introduction goes here
</p>
<form action = "xyz.php" method = "post" name = "downloadform">
<input type="submit" value="Download" name="dwnld_file"/>
</form>
';
if (isset($_POST['dwnld_file'])) {
mysql_connect("localhost", "root", "");
mysql_select_db("PQR");
$res = mysql_query("Select * from tab1");
while ($row = mysql_fetch_array($res)) {
$file = '$row["col2"]';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $row["col2"] . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Content-Length:' . filesize($file));
readfile($file);
}
}
include 'Template.php';
?>
Error:
My pdf file is getting downloaded but upon opening it it says "failed to load pdf document"
Please help where i am wrong.
***Edit****
I tried different approach and my file is downloading but still it says "failed to load pdf document"
My other approach code is below
<form action = "xyz.php" method = "post" name = "downloadform">
<input type="submit" value="Download " name="dwnld_file"/>
</form>
<?php
if (isset($_POST['dwnld_file'])) {
mysql_connect("localhost", "root", "");
mysql_select_db("test");
$res = mysql_query("Select * from tab1");
while ($row = mysql_fetch_array($res)) {
$file = $row["col1"];
echo $file;
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' .$file. '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Content-Length:' . filesize($file));
readfile('myfile/'.$file);
}
}
?>
Please tell me if i am doing anything wrong.
I implemented another solution after doing some more study and i was able to solve the problem.
Below is my code i used to implement.
Concept:
1) We can use HTML+JSP+PHP to get this functionality.
2) At HTML + JSP side we need to call eventListener to capture the event of clicking the button.
3) This event will redirect to the php page which will download the pdf file to your local computer.
Code Snippet:(here XYZ is the name of page where you want to show Download button)
in XYZ.php
<input type="button" id="btnshow" value="Download" name="dwnld" />
<script>
var btn = document.getElementById('btnshow');
btn.addEventListener('click', function () {
document.location.href = '<?php echo 'download.php'; ?>'
})
</script>
in download.php
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("database_name");
$res = mysql_query("Select * from table_name");
while ($row = mysql_fetch_array($res)) {
$file = 'Pdf_File/' . $row["Path"];
$filename = $row["Path"];
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
readfile($file);
}
?>
Hope it help some one out there newbie like me.
Happy Coding!
The following code is mine.
I want to download a file and echo together in Php. But echo in end of code doesn't work, how to fixed this code?
$filename = trim($conf['savedir']).$_GET['task'].".".$_GET['hash'].".xml";
$downname = $_GET['task'].'.'.$_GET['hash'].'.xml';
$fp = fopen($filename, 'r');
if($fp == null)
{
echo "Wrong Access1";
} else {
header('Content-type:text/xml charset=utf-8');
header('Content-disposition: attachment; filename='.$downname);
header('Content-length:'.filesize($filename));
header('Content-transfer-encoding: binary');
header('Pragma: no-cache');
header('Expires: 0');
fpassthru($filename);
fclose($fp);
echo $_GET['task'].'.'.$_GET['hash'].'.xml file download.';
}
Like #Mark Backer said, you can't echo and download at the same time for the same request, but there are some "tricks" to echo something and download the file.
Let's suppose that your code from the question is located into test.php file and the user requests the file that contains the code bellow:
<script type="text/javascript">
window.open('test.php?task=<?=$_GET['task']?>&hash=<?=$_GET['hash']?>', '_blank');
</script>
<?php
echo $_GET['task'].'.'.$_GET['hash'].'.xml file download.';
So, the javascript code will pass the $_GET variables to test.php open a new window for download and in echo your message in the current window.
<?php
include "function.php";
$account = findSomeFile();
$file_url = '/var/www/html/tvconfig/netflix/'.$account.'.zip';
echo $account;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
?>
Above is my PHP script. When I load the page I get the download prompt in the browser but there is no echo on the page. I have tried placing the echo after the readfile function but it does not work.
You can play a little trick:
<?php
$account = "38950596";
$file_url = "$account.json";
echo "Hello Friend!\n";
$fc = file_get_contents($file_url);
?>
<script type="text/javascript">
window.onload = function(e) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent('<?php echo $fc ?>'));
pom.setAttribute('download', '<?php echo $account ?>');
pom.click();
}
</script>
This will create a anchor element and as soon as the page loads, it will force a file download by using the click event.
Demo: http://main.xfiddle.com/e1a35f80/38950596_stackoverflow.php
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"));