- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to force file download with PHP?
The below code can be used to force a file to get downloaded in PHP.
<?php header('Content-type: text/javascript'); header('Content-Disposition: attachment; filename="file.js"'); readfile(file that is downloaded.js'); //This can be printed for verification purpose ?>
NOTE − This needs to be done before displaying any output, otherwise the file will have output from other operations as well (which might be irrelevant).
Another method that could be used is to use the .htaccess solution. In this method, all the files on the server can be forced to be downloaded, and can be added to the .htaccess file. This has been demonstrated below −
AddType application/octet-stream csv header('Content-Type: application/csv'); header('Content-Disposition: attachment; filename=name of csv file'); header('Pragma: no-cache'); readfile("path-to-csv-file");
- Related Articles
- Download file through an AJAX call in PHP
- How to download a file using Node.js?
- How to download large files through PHP script?
- Upload file with php to another php server
- How To Raise a "File Download" Dialog Box in Python?
- How to download a file from a URL in C#?
- How to force write of file with filedescriptor fd to disk using Python?
- How to raise a "File Download" Dialog Box in Python CGI Programming?
- How to style download buttons with CSS?
- Access to file download dialog in Firefox in Selenium.
- How to create a download link with HTML?
- How to read a single file inside a zip archive with PHP
- How to display errors in PHP file?
- How to echo XML file in PHP
- How to import csv file in PHP?

Advertisements