PHP output file on disk to browser
There is fpassthru() that should do exactly what you need. See the manual entry to read about the following example:
<?php
// open the file in a binary mode
$name = './img/ok.png';
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
// dump the picture and stop the script
fpassthru($fp);
exit;
?>
See here for all of PHP's filesystem functions.
If it's a binary file you want to offer for download, you probably also want to send the right headers so the "Save as.." dialog pops up. See the 1st answer to this question for a good example on what headers to send.
To output a file to a user's browser, does it need to be saved on disk first? PHP
if you have no other output then it is possible see.
serving pdf file using php header produces the pdf source instead of the file
using the headers and just outputting your array, in a fomat you wish.
To output a file to a user's browser, does it need to be saved on disk first? PHP
if you have no other output then it is possible see.
serving pdf file using php header produces the pdf source instead of the file
using the headers and just outputting your array, in a fomat you wish.
PHP: how to output to browser a png image from disk?
try:
header("Content-type: image/png");
passthru("cat $full_file_name");
Be very careful with the passthru command if you're working with user input, since this function will execute anything you pass it.
https://www.php.net/manual/en/function.passthru.php
When allowing user-supplied data to be passed to this function, use escapeshellarg() or escapeshellcmd() to ensure that users cannot trick the system into executing arbitrary commands.
PHP Word, send generated file to browser as output without saving it on disc
This below example maybe works, but to get the expected behaviour of the download in all browsers, the Content-Type
header has to be correct for Word2007. Maybe you will need some additional headers for proper output.
You can try this:
$filename = "YOUR_Filename.docx";
header( "Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document" );// you should look for the real header that you need if it's not Word 2007!!!
header( 'Content-Disposition: attachment; filename='.$filename );
$h2d_file_uri = tempnam( "", "htd" );
$objWriter = PHPWord_IOFactory::createWriter( $phpword_object, "Word2007" );
$objWriter->save( "php://output" );// this would output it like echo, but in combination with header: it will be sent
Thanks to @jamsandwich: The correct header for Word 2007 should be application/vnd.openxmlformats-officedocument.wordprocessingml.document
.
Reference Microsoft
How to force a file to download in PHP
If you want to force a download, you can use something like the following:
<?php
// Fetch the file info.
$filePath = '/path/to/file/on/disk.jpg';
if(file_exists($filePath)) {
$fileName = basename($filePath);
$fileSize = filesize($filePath);
// Output headers.
header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileName);
// Output file.
readfile ($filePath);
exit();
}
else {
die('The provided file path is not valid.');
}
?>
If you simply link to this script using a normal link the file will be downloaded.
Incidentally, the code snippet above needs to be executed at the start of a page (before any headers or HTML output had occurred.) Also take care if you decide to create a function based around this for downloading arbitrary files - you'll need to ensure that you prevent directory traversal (realpath is handy), only permit downloads from within a defined area, etc. if you're accepting input from a $_GET or $_POST.
PHP Send A PDF To Clients Browser Without Writing It To Disk (Server Side)
This was failing because I had some output before this part of the document.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div>
"navigation bar goes here"
</div>
<?php
$data = base64_decode($origSel['InvestigationFile']);
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="Statement"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
echo $data;
?>
After removing all the HTML, I was able to get this to work.
Related Topics
Leverage Browser Caching for 3Rd Party Js
New Limit Within PHP: 1000 Fields Per Post. Does Someone Know, If the Number Can Be Influenced
Get the Latest File Addition in a Directory
Manipulate an Archive in Memory with PHP (Without Creating a Temporary File on Disk)
PHP Link to Image File Outside Default Web Directory
How to Get MAC Address of Client Using PHP
How to Connect to Mssql Using Pdo Through PHP and Linux
Jquery Ajax Form Using Mail() PHP Script Sends Email, But Post Data from HTML Form Is Undefined
PHP Function to Get Mp3 Duration
Setcookie, Cannot Modify Header Information - Headers Already Sent
PHP Header() Redirect with Post Variables
How to Write JavaScript Code Inside PHP
PHP Unserialize Fails with Non-Encoded Characters