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"));
Related
I am writing a website using bootstrap 4, html 5 and php 7.
The website handle a folder with many pdf files inside.
In order to control the access to the pdf file, i use a php file that output the pdf wanted.
The folder of pdf is protected with a htaccess file.
Everything is working perfectly expect on some smartphones which display a white screen instead of the pdf file.
Same for the download option.
For debugging, I added logs at each steps,
In the normal situation, where i get the pdf, i got "-9-13-"
In the situation with the white screen, i got "-9-13-16", so something weird with the "exit"
Any ideas ?
if (file_exists($file))
{
//log_dbg('9-');
if(isset($_POST['download']))//download
{
//log_dbg('10-');
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . $chapter_file_name . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
readfile($file);
//log_dbg('11-');
exit;
//log_dbg('12-');
}
else //Stream
{
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $chapter_file_name . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
readfile($file);
//log_dbg('13-');
exit;
//log_dbg('14-');
}
}
else
{
//log_dbg('15-');
echo 'Fichier '.$file.' introuvable';
}
//log_dbg('16-');
Do you have any idea what can be the problem ?
You can try in real here :
http://laguildedesecrivains.fr/story.php -> click on "Chapitre 1"
I am using following code for view pdf file in browser but file is not showing correctly.
<?php
$file = 'google.pdf';
$filename = 'google.pdf';
header('Content-type:application.pdf');
header('Content-Disposition: inline; filename"' . $filename .'"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
echo #file_get_contents($file);
?>
Code output:
Please help! Thanks in Advance
That's an invalid content type, you should be using application/pdf
// change this
header('Content-type:application.pdf');
// to this
header('Content-Type: application/pdf');
i have this show uploaded pdf file on my localhost my view is
<?php
// retrieve your info ecc.
$url = $content->PATH;
$filename = $content->FILE_NAME;
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename. '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
#readfile($url);
?>
just to be clear
$content->PATH and $content->FILE_NAME are data from database
$content->FILE_NAME is $file_name = $file_data['file_name'];
and $content->PATH = base_url().'uploads/pdf/'.$file_name;
now it perfectly works on my localhost, but on the server, it looks like this
the link to this is
$row[] = '  Show File";
what is the error of this ? how can i solve this?
Try with CI built-in download helper class
$this->load->helper('download');
force_download($file_name);
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;
?>
I have some pdf files on a storage server and i want to show them to the users without showing them the real file path, and i want to do this with PHP.
I tryed this:
$file = 'https://storage.server_test.com/agc/catalogs/catalog1.pdf';
$filename = 'catalog.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);
But the pdf is not displayed at all and i get this error: This PDF document might not be displayed correctly.
What am i doing wrong?
Have you tried embedding it using html object tag?
<object data="https://storage.server_test.com/agc/catalogs/catalog1.pdf" type="application/pdf">
<embed src="https://storage.server_test.com/agc/catalogs/catalog1.pdf" type="application/pdf" />
</object>
Alternatively in php, try this header:
header('Content-Disposition: attachment; filename="' . $filename . '"');
Finally, if #readfile($file) isn't working, try:
echo #file_get_contents($file);
After trying this myself locally, I think I managed to get it working. Try this:
<?php
$remote_pdf = 'http://www.saronicferries.gr/content/pdfFiles/sample.pdf';
$remote_file_name = end(explode('/', $remote_pdf));
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'. $remote_file_name .'"');
header('Content-Transfer-Encoding: binary');
readfile($remote_pdf);
?>
Output:
I think what was the problem is that filesize($file) was failing for remote file. After removing that and header('Accept-Ranges: bytes');, it works.