问题描述
我正在构建一个允许用户下载一些pdf文件的应用程序,但是这些文件存储在另一台受密码保护的服务器上,密码是已知的。
是将这些文件传递给用户的最佳方法。
I am building an application that will allow users to download some pdf files, but these files are stored on a different server which is password protected, the password is known.What is the best way to pass these files on to the user.
MyAppIP/applicationfiles.php
FilesIP/PDF-Files/file1.pdf <-- this is password protected, but I know the pass.
我正在考虑先将文件保存在自己的服务器上,因为文件的最大大小为100kb。然后将它们传递给用户并再次删除本地文件,但是我还没有完全弄清楚如何首先从其他服务器获取文件。
那么如何从目录中获取文件。
I was thinking on saving the files on my own server first since they have a maximum size of 100kb. Then passing them to the user and deleting the local file again, but I haven't completely figured out how to obtain the file from the other server in the first place.
So how do I obtain files from the different server in php?
推荐答案
您可以使用CURL从受保护的服务器下载文件。 CURL还支持许多授权方法。
You can use CURL for download file from protected server. CURL also support many authorization methods. http://www.php.net/manual/en/function.curl-setopt.php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'FilesIP/PDF-Files/file1.pdf');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "Login:Password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$file = curl_exec($ch);
curl_close($ch);
file_put_contents('/var/www/file.pdf', $file);
// return download link to user
// <a href="file.pdf">Download</a>
这篇关于PHP从其他受密码保护的服务器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!