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

末鹿安然 提交于 2019-12-11 15:53:36

问题


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.


回答1:


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.)




回答2:


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.




回答3:


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();
?>


来源:https://stackoverflow.com/questions/541265/renaming-file-on-another-server-as-user-downloads-it-2-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!