简体   繁体   English

用户下载文件时在另一台服务器上重命名文件[2]-使用PHP

[英]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. 我今天已经问过这个问题,但是这次我想知道是否可以通过PHP实现此功能,因为Javascript还不行。

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'. 我想这样做,以便可以更改下载文件名,因为它始终是“ file.zip”。

Is this possible with PHP? 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). 您可以使用curl将文件下载到服务器并正确提供文件(带有Content-Disposition标头)。 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. 只要使用HTTP,就无法仅发送标头,而让另一台服务器将内容直接流式传输到客户端。

You could do this, and you can do it in several ways. 您可以执行此操作,并且可以通过多种方式执行。

1) (simple) copy the file to your server, and rename it. 1)(简单)将文件复制到您的服务器,然后重命名。 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. 2)(更难)创建一个名为stub的php文件,从php中的远程服务器读取该文件,并将内容流式传输到脚本输出。 This will need you to set appropriate headers, etc. as well as setting up your webserver to parse through PHP. 这将需要您设置适当的标头等,以及设置网络服务器以通过PHP进行解析。

Seriously, I'd go with option 1. (assumes you have a legal right to serve the content, etc.) 认真地说,我会选择选项1。(假设您具有提供内容的合法权利,等等)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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