kpp
kpp

Reputation: 826

PHP Downloading a file from a different password protected server

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.

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?

Upvotes: 0

Views: 1719

Answers (2)

Ilya Manyahin
Ilya Manyahin

Reputation: 238

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>

Upvotes: 3

Droga Mleczna
Droga Mleczna

Reputation: 511

If the auth is basic http and you have installed curl, you can use this:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
curl_close($ch);

Upvotes: 0

Related Questions