Jump to content

Download script problem


moon_php

Recommended Posts

Not 100% sure that my problem is PHP related but here goes:

 

Background:

I have a php download script (see below) that hides the real location of the files and creates a download log.

Some of the download-able files are mirrored on remote servers.

 

Problem:

When downloading one of the mirrored files, the dialog ('open'/'save as') still says that it is downloaded from my site e.g. "from: http://www.mysite.com"!

 

Question:

Is there a way to change my script to display the real host of the mirrored file?

 

<?php

require ("dl_filenames.php"); //Get array of all valid filenames
define('LOG_DOWNLOADS',true); // log downloads?  true/false
define('LOG_FILE','dl_logfile.log'); // log file name
$fname = basename(@$_GET['f']); // get filename
if (in_array($fname, $filename,true)) {

$path = "http://somesite.com/files/"; // path to files
$fullPath = $path.$fname;

$remoteFile = $fullPath;
$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
echo 'cURL failed';
exit;
}

$contentLength = 'unknown';
$status = 'unknown';
if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
$status = (int)$matches[1];
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
$contentLength = (int)$matches[1];
}

if ($fd = @fopen ($fullPath, "rb")) {
//	$fsize = filesize($fullPath);
$fhost = parse_url($path, PHP_URL_HOST);
$path_parts = pathinfo($fullPath);
//	$filehost = $path_parts["dirname"];
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
	case "zip";
	header("Content-type: application/zip");
	header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
	break;
	case "rar":
	header("Content-type: application/x-rar-compressed");
	header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
	break;
	case "7z":
	header("Content-type: application/x-7z-compressed");
	header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
	break;
	default;
	header("Content-type: application/octet-stream");
	header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
	}
header("Content-length: $contentLength");
header("Content-Location: $fhost");
//	header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
	$buffer = fread($fd, 2048);
	echo $buffer;
	}
}
@fclose ($fd);

// log downloads
if (!LOG_DOWNLOADS) die();

$f = @fopen(LOG_FILE, 'a+');
if ($f) {
@fputs($f, date("d.m.Y g:ia").",".$_SERVER['REMOTE_ADDR'].",".$fname.",".$path."\n");
@fclose($f);
}
}

exit;
?>

Link to comment
https://forums.phpfreaks.com/topic/199619-download-script-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.