Home > Backend Development > PHP Tutorial > How to Force File Downloads in PHP from Remote Storage?

How to Force File Downloads in PHP from Remote Storage?

Susan Sarandon
Release: 2024-11-15 01:46:02
Original
589 people have browsed it

How to Force File Downloads in PHP from Remote Storage?

Forcing File Downloads in PHP with Remote File Storage

To add a "Download this File" feature to a website while preventing direct playback in the browser, consider the following PHP solution:

// Locate the remote file.
$file_name = 'file.avi';
$file_url = 'http://www.myremoteserver.com/' . $file_name;

// Configure the download settings.
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename="' . $file_name . '"');

// Download the remote file content.
readfile($file_url);

// Ensure no output follows the download.
exit;
Copy after login

First, the PHP script locates the remote file and sets up the necessary HTTP headers to force a download instead of playback.

The script uses the readfile() function to retrieve the remote file's content. Note that the fopen_wrappers setting must be enabled for PHP to read remote URLs.

Finally, the script exits to prevent any further output that could interfere with the download process.

The above is the detailed content of How to Force File Downloads in PHP from Remote Storage?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template