Download PDF file from database using PHP [duplicate] - php

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

Related

downloading pdf files using php [duplicate]

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.

Alternating downloads with php [duplicate]

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.

PHP: How to make browser to download file on click

PHP Beginner. File uploading is successful but my browser doesn't download the files, instead it reads the file. So i referred other threads and found below code which is not working. I want to download files when i click on the hyperlink download. Selected the path from MySQL database.
$rows = mysqli_num_rows($result);
if($rows>0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<div> <?php echo $row['Object_Name'];?>
<a href="<?php
$file_url = $row['Object_Path'];
header('Content-Type: application/octet-stream');
header("Content-disposition: attachment; filename=\"".$row['Object_Name']. "\"");
readfile($file_url);
?>">Download</a><br>
</div>
<?php
}
}
In a paged called download.php, have the following code:
<?php
$filename = 'file.pdf';//this should be the name of the file you want to download
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // required for certain browsers
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'. basename($filename) . '";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filename));
readfile($filename);
exit;
?>
Your main page should then have a link to the download page like this:
DOWNLOAD
Let me know if that works for you.
Edited:
My previous example was for the download of a pdf file. In the case that you want to download a different type of file, a few lines have to be slightly modified. I recommend you first try downloading a pdf file with the previous code, and after having accomplished that testing out on other files.
To retrieve the path from the database, you can use MySQL (PDO).
$sqlStatement = "SELECT path FROM my_table WHERE some_id = ".$something;
/*if you are retrieving the path from the database,
you probably have a lot of different paths available
there, so only you know the criteria which will decide
which of the many paths it is that you choose to extract*/
$sqlPrepared = $connection->prepare($sqlStatement);
$sqlPrepared->execute();
$row_info = fetch($sqlPrepared);
$filename = $row_info['path'];// this would be the $filename = 'file.pdf'
//that was in the example above
If you are not sure how to connect to the database, there are a lot of articles online explaining MySQL that is relatively straightforward.
I hope that helped :)
You have to use two separate files.
In link page, you can output a HTML like this:
Download file 1
Download file 2
Download file 3
(...)
You can use a <form>, if you prefer.
Then, in download.php:
Select appropriate file using GET/POST parameter ($_GET['file'] in above example);
send appropriate headers (like in your original code);
echo your file (you can use readfile);
Mandatory: no additional output in this script! Even a single additional space will corrupt downloaded file.

PHP how to open the .doc and .docx files? [duplicate]

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.

Force user to download file in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Forcing to download a file using PHP
When we need to force user to download a file, we use header with several parameters/options. What if I use
header("location:test.xlsx");
This is working :) Is there any drawback of using this shortcut ?
This approach should solve the problems mentioned here
download.php?filename=test.xlsx
if isset ($_GET['filename']){
$filename = $_GET['filename']
}
else{
die();
}
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');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
And of course don't forget to secure this so users can't download other files
There are a few disadvantages to this method:
If the file is one the browser can read, it won't be downloaded (like .txt, .pdf, .html, .jpg, .png, .gif and more), but simply be shown within the browser
Users get the direct link to the file. Quite often, you don't want this because they can give this link to others, so...
it will cost you more bandwidth
it can't be used for private files
if it's an image, they can hotlink to it
All you're doing is redirecting to a file. This is no different than if they went to it directly.
If you are trying to force a download, you need to set your Content-Disposition header appropriately.
header('Content-Disposition: attachment');
Note that you can't use this header when redirecting... this header must be sent with the file contents. See also: https://stackoverflow.com/a/3719029/362536
Not every file is forced to download.
If you were to use that header() on a .jpg the browser won't open the download dialog but will just show the image.

Categories