how to create Downloadable links in php? [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have used file system to store the files, all files are either word or pdf files. how should i create their downloadable links on client side.
Any help would be appreciated.

you can download file using:
<?php
$file = 'filename.extension';
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));
ob_clean();
flush();
readfile($file);
exit;
}
?>

You can simply write the file path in the href attribute of the <a /> tag...
Download Here

<?php
if (isset($_GET['file'])) {
$file = $_GET['file'];
if (file_exists($file) && is_readable($file) && preg_match('/\.pdf$/',$file)) {
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($file);
}
} else {
header("HTTP/1.0 404 Not Found");
echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>";
}
Try the above snippet and name it as download.php!
Once the above has been done, save the file and if needed upload to the server hosting your web page.
Finally, once uploaded, all future .PDF links that you want to be downloaded instead of opened in the browser will need to point to download.php?file=example.pdf, where example.pdf is the name of the PDF you want for the user to download. Below is an example of what a full link could look like.enter code here
Click here to download PDF

Download
This should do the job.

Related

Viewing a file when clicked not downloaded? [duplicate]

This question already has answers here:
What is correct content-type for excel files? [duplicate]
(3 answers)
Closed 3 years ago.
When I do a Google search I find a TON of replies about the code needed to download a file when clicked and that works just fine. What I am looking for is when I click on the link it will just open up the file in the browser instead of prompting to download the file. Here is the working code to Download a file:
$file = $invoice_dir . '/test.xlsx';
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;
How do I just make it open up instead using my local software? It is an XLSX Office type file not plain text. I tried include() and it outputs jibberish.
You just needed to open the file in a href like this
<a href"http://www.yourdomain.com/index.php?file=file.pdf">View File</a>
and in case you want to open it in a new browser tab then
<a href"http://www.yourdomain.com/index.php?file=file.pdf" target="_blank">show file</a>
Add this code
header("Content-type: application/vnd-ms-excel");

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.

pdf file saved as current page source

im trying to make pdf files downloadable in my website but all im getting is the current page source(html).
the file name is correctly given but the file itself is not downloading.
ive tried various fixes found on stackoverflow but its not helping.
ive tried AddType application/octet-stream .pdf in htaccess , also ForceType.
Tried the php fix here:
How to make PDF file downloadable in HTML link?
and going through php with this:
header("Content-disposition: attachment; filename=filename.pdf");
header("Content-type: application/pdf");
readfile("filename.pdf");
and then linking to the php file, still the same.
what am i doing wrong and what information do you require to make better sense of this?
You can have serveral mistakes to check (and debug) try this
<?php
$file = ABSOLUTE_PATH_WHERE_PDF_IS_STORED.'/my.pdf'; //replace *ABSOLUTE_PATH_WHERE_PDF_IS_STORED* with your path
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;
} else {
die("FILE [".$file."]" don't exists!");
}
?>
So i found the problem, which is of course quite obvious, i linked to the files wrongly, and since im using a cms it sent the front page source (default behaviour).
Peculiarly the html5 download attribute doesn’t work anyway.
Thank you Donald123 and PKa for answering.
You can use a tag with attribute download
<a href="path/to/file/*.pdf" download>Download this pdf</a>
Its a Quick way to do that.

Downloading a file from a PHP server via a website [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have physical files which I want users to download on my website. The files are located at:
C:/xampp/htdocs/myfile/uploads/*
I need a PHP script which can download files dynamically on click. Let's say I have the following button which when clicked it triggers magic.php script.
Download file
What PHP code do I need in magic.php to download the file name I passed in the query parameters?
Download link
Download
magic.php page
<?php
$file = 'C:/xampp/htdocs/myfile/uploads/'.urldecode($_GET['file']);
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;
}
?>
Maybe you could do something like this: (Correct me if i am wrong)
<a href="uploads/<?php echo $row['name']; ?>" download="download">

Allowing downlaod after payment [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
i am confused in how to allows download the file.zip after the payment.If i redirect them to a download page where is file is placed in sever they can download the file again easily or they can pass that link to anyone.
Any suggestions please!
Don't use a direct link to the file - use a PHP file that serves the file up as a download, but only if a certain session var is found (created in the confirmation of the payment process)
Just like #SmokeyPHP mentioned, just output the file through PHP instead of linking to it directly.
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
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);
exit;
}
?>
http://php.net/manual/en/function.readfile.php
This way you have a full control over who downloads what. Of course depending on the file size, you may wish to split the file into smaller chunks. You don't want to be buffering 40 MB files in your server memory every 5s. With bigger files, you can use something like this:
<?php
$file = fopen("file.dat", "r");
while (!feof($file)) {
echo fgets($file);
}
fclose($file);
?>
put .zip file outside the webserver;
protect the .zip URL with some rule [e.g. being logged in and having purchased the resource];
associate the .zip URL with an action that reads the actual binary file and forwards it to user [plenty of examples in here].

Categories