This question already has answers here:
How can I view/open a word document in my browser using with PHP or HTML
(6 answers)
Closed 7 years ago.
doc files
$filename = 'ahfdghasfdh.doc';
header('Content-type: application/msword');
header('Content-Disposition: inline; filename="testqq"');
#readfile($filename);
I tried this code its not working. How to open the .doc files on browser using php.
Try below code.
<?php
$filename = 'ahfdghasfdh.doc';
header('Content-disposition: inline');
header('Content-type: application/msword'); // not sure if this is the correct MIME type
readfile($filename);
exit;
?>
You can open as like that:
MSWORD FILE
And other solution #vinod already shared
And if you just want to open doc file on browser no application involved than I recommend to use GOOGLE DOCS.
Related
This question already has answers here:
PHP output file on disk to browser
(6 answers)
PHP: How to make browser to download file on click
(2 answers)
Closed 5 years ago.
I have PHP files stored on my server, and their names in the mysql database, I want to download those files. What code should I write for the same? I am using PHP as coding language. Please help.
<?php
$file = 'send_me.pdf';
if (file_exists($file)) {
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;
}
?>
Obviously, set $file to the file name.
Read more about the use of readfile here.
Download?
Literally just make a link the stored file.
file_put_contents("PDFName.pdf", fopen("http://someurl/PDFName.pdf", 'r'));
You really should show what you have done so far/researched online before asking a question!
This will download the file PDFName.pdf from the url http://someurl/PDFName.pdf and put it into the same directory as the script is in.
This question already has answers here:
How can I create a download link of a PDF that does not require a right click?
(6 answers)
Closed 6 years ago.
I have this code, that working fine :
$files = "files1.rar";
header('Content-Description: File Transfer');
header('Content-Type: application/x-rar-compressed');
header('Content-Disposition: attachment; filename="your_download.rar" ');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($files));
ob_clean();
flush();
readfile($files);
exit;
But I would like to add some other files, maybe with an array like :
$files=array('1'=>"files1.rar", '2'=>"files2.rar");
With a download link like download.php?docid=1
And an expired date based on the file's upload + one week.
Thanks
Before you start the download, the best way - in my opinion - is to create a zip file on the server and download this one.
From my point of view, it is not possibe to download files on the way you have choose.
Here is a link to the php dokumentation on the Zip process in PHP.
http://php.net/manual/de/zip.examples.php
On Git, there is an example.
This question already has answers here:
How to download a php file without executing it?
(6 answers)
Closed 7 years ago.
I have a website, and I want to be able to put a link that enables the user to download a php file, I don't want it to run or for the user to navigate to the url, but be able to download the original file, is this possible?
Thanks
Thanks Biswajit, that link got me what I needed (I saw the original "duplicate" but I wasn't working for me)
<?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.');
}
?>
This question already has an answer here:
Files sometimes download as .PHP instead of .PDF?
(1 answer)
Closed 4 years ago.
I have to download most recent uploaded PDF file from MySQL database using PHP. The file can view but while saving it to local folder, instead of saving it as .pdf , it saves .php. and that .php file contains encoded data.
Can anyone suggest how I download/save .pdf file? Code is:
<?php
include 'connection.php';
$sql=mysqli_query($connection,"Select name,content from ekalp where id = (select max(id) from ekalp)");
$result=mysqli_fetch_assoc($sql);
//$resu=$result['name'];
$result=$result['content'];
echo $result."<br>";
$filename = $result.'pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
ob_clean();
ob_flush ();
#readfile($filename);
mysqli_close($connection);
?>
See this solution, reproduced here:
Adding ob_clean(); and flush(); functions before the readfile(); function, could be something worth using, as per what the PHP manual states on the subject.
readfile() http://php.net/manual/en/function.readfile.php
ob_clean() http://php.net/manual/en/function.ob-clean.php
flush() http://php.net/manual/en/function.ob-flush.php
These functions are not present in your posted code
I am assuming $resu holds a uploaded file name. Then why you just don't link to file ??
<?php
include 'connection.php';
$sql=mysqli_query($connection,"Select name,content from ekalp where id = (select max(id) from ekalp)");
$result=mysqli_fetch_assoc($sql);
$resu=$result['name'];
?>
Download File
This question already has answers here:
php - How to force download of a file?
(7 answers)
Closed 7 years ago.
I have a php page with information, and links to files, such as pdf files. The file types can be anything, as they can be uploaded by a user.
I would like to know how to force a download for any type of file, without forcing a download of links to other pages linked from the site. I know it's possible to do this with headers, but I don't want to break the rest of my site.
All the links to other pages are done via Javascript, and the actual link is to #, so maybe this would be OK?
Should I just set
header('Content-Disposition: attachment;)
for the entire page?
You need to send these two header fields for the particular resources:
Content-Type: application/octet-stream
Content-Disposition: attachment
The Content-Disposition can additionally have a filename parameter.
You can do this either by using a PHP script that sends the files:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment');
readfile($fileToSend);
exit;
And the filenames are passed to that script via URL. Or you use some web server features such as mod_rewrite to force the type:
RewriteEngine on
RewriteRule ^download/ - [L,T=application/octet-stream]
Slightly different style and ready to go :)
$file = 'folder/' . $name;
if (! file) {
die('file not found'); //Or do something
} else {
// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
// Read the file from disk
readfile($file);
}