简体   繁体   中英

Renaming File on another server as user downloads it [2] - using PHP

I have asked this question today already but this time I want to know if I can achieve this via PHP since Javascript wasn't up to it.

I have a link to a file on another server. If i provide this link to my users the headers are pushed out to download that file from that server.

Is there a way for me to capture those headers and file and redirect the download to the user? I would like to do this so that I can change the filename of the download since it is always 'file.zip'.

Is this possible with PHP?

Thank you for any help.

You can download the file to your server using curl and serve it correctly(with a Content-Disposition header). As long as you are using HTTP, there's no way to send just the header and let another server stream the content directly to the client.

You could do this, and you can do it in several ways.

1) (simple) copy the file to your server, and rename it. Point your download links to this copy.
2) (harder) Create a stub php file, called , read the file from the remote server within php, and stream the content to the script output. This will need you to set appropriate headers, etc. as well as setting up your webserver to parse through PHP.

Seriously, I'd go with option 1. (assumes you have a legal right to serve the content, etc.)

Maybe you can use a script similar to the following one:

<?php
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: http://www.example.com/the_path/file.zip");
  header('Content-Disposition: attachment; filename="alternate_filename.zip"');
  exit();
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM