Skip to content

Instantly share code, notes, and snippets.

@iantho
Created June 28, 2015 01:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save iantho/9ca3cfa1be51b1429e53 to your computer and use it in GitHub Desktop.
HTTP Basic Authentication Protected File Downloader
<?php
/*
The MIT License (MIT)
Copyright (c) 2013 Ian Thomas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// PARAMETERS ==================================================================
$filename = "example.tar.gz";
$parent_url = "https://www.server.com/path";
$username = "user";
$password = "password";
// MAIN ========================================================================
// ensure PHP has enough memory allocated to cache the entire download...
ini_set('memory_limit', '128M');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $parent_url . "/" . $filename);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
// reference: http://www.php.net/manual/en/function.curl-setopt.php#98164
// extra options to work with SSL...
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// other options - not usually required...
// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
// curl_setopt($curl, CURLOPT_HEADER, true);
// curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
// curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
// download the file contents from the host...
$data = curl_exec($curl);
if ( $data === false || curl_errno($curl) )
{
die("Download error: " . curl_error($curl) );
}
$info = curl_getinfo($curl);
curl_close($curl);
// check for authentication error...
if ( $info['http_code'] == 401 )
{
die("Password authentication error");
}
// prepare file download for client browser...
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . strlen($data));
ob_clean();
flush();
echo $data;
flush();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment