抱歉打扰你。我有下载脚本。它是从远程服务器下载到本地服务器,然后发送给用户下载。主站点从文件服务器获取文件,然后将其提供给用户。我希望主站点从远程服务器传递直接链接。
我不知道如何将其直接发送给用户。现在它在我的网站上造成负载和带宽问题。任何想法,如何做到这一点。
public function download()
{
// remove session
if (isset($_SESSION['showDownload']))
{
// reset session variable for next time
$_SESSION['showDownload'] = null;
unset($_SESSION['showDownload']);
session_write_close();
}
// php script timeout for long downloads (2 days!)
set_time_limit(60 * 60 * 24 * 2);
// load the server the file is on
$storageType = 'local';
$storageLocation = _CONFIG_FILE_STORAGE_PATH;
$uploadServerDetails = $this->loadServer();
if ($uploadServerDetails != false)
{
$storageLocation = $uploadServerDetails['storagePath'];
$storageType = $uploadServerDetails['serverType'];
// if no storage path set & local, use system default
if ((strlen($storageLocation) == 0) && ($storageType == 'local'))
{
$storageLocation = _CONFIG_FILE_STORAGE_PATH;
}
}
// get file path
$fullPath = $this->getFullFilePath($storageLocation);
// open file - via ftp
if ($storageType == 'remote')
{
// connect via ftp
$conn_id = ftp_connect($uploadServerDetails['ipAddress'], $uploadServerDetails['ftpPort'], 30);
if ($conn_id === false)
{
$this->errorMsg = 'Could not connect to ' . $uploadServerDetails['ipAddress'] . ' to upload file.';
return false;
}
// authenticate
$login_result = ftp_login($conn_id, $uploadServerDetails['ftpUsername'], $uploadServerDetails['ftpPassword']);
if ($login_result === false)
{
$this->errorMsg = 'Could not login to ' . $uploadServerDetails['ipAddress'] . ' with supplied credentials.';
return false;
}
// prepare the stream of data
$pipes = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
if ($pipes === false)
{
$this->errorMsg = 'Could not create stream to download file on ' . $uploadServerDetails['ipAddress'];
return false;
}
stream_set_write_buffer($pipes[0], 10000);
stream_set_timeout($pipes[1], 10);
stream_set_blocking($pipes[1], 0);
$fail = false;
$ret = ftp_nb_fget($conn_id, $pipes[0], $fullPath, FTP_BINARY, FTP_AUTORESUME);
}
// open file - locally
else
{
$handle = @fopen($fullPath, "r");
if (!$handle)
{
$this->errorMsg = 'Could not open file for reading.';
return false;
}
}
// download speed
$speed = 0;
// if free/non user
$Auth = Auth::getAuth();
if (($Auth->loggedIn == false) || ($Auth->level == 'free user'))
{
$speed = (int) SITE_CONFIG_FREE_USER_MAX_DOWNLOAD_SPEED;
}
else
{
$speed = (int) SITE_CONFIG_PREMIUM_USER_MAX_DOWNLOAD_SPEED;
}
// do we need to throttle the speed?
if ($speed > 0)
{
// create new throttle config
$config = new ThrottleConfig();
// set standard transfer rate (in bytes/second)
$config->burstLimit = $speed;
$config->rateLimit = $speed;
// enable module (this is a default value)
$config->enabled = true;
// start throttling
$x = new Throttle($config);
}
// output some headers
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: " . $this->fileType);
header("Pragma: public");
header("Content-Disposition: attachment; filename=\"" . str_replace("\"", "", $this->originalFilename) . "\"");
header("Content-Description: File Transfer");
header("Content-Length: " . $this->fileSize);
// output file - via ftp
if ($storageType == 'remote')
{
while ($ret == FTP_MOREDATA)
{
$contents = stream_get_contents($pipes[1]);
if ($contents !== false)
{
echo $contents;
flush();
}
$ret = ftp_nb_continue($conn_id);
}
/*
$contents = stream_get_contents($pipes[1]);
if($contents !== false)
{
echo $contents;
flush();
}
*/
fclose($pipes[0]);
fclose($pipes[1]);
}
// output file - local
else
{
while (($buffer = fgets($handle, 4096)) !== false)
{
echo $buffer;
}
fclose($handle);
}
exit();
}
public function loadServer()
{
// load the server the file is on
if ((int) $this->serverId)
{
// load from the db
$db = Database::getDatabase(true);
$uploadServerDetails = $db->getRow('SELECT * FROM file_server WHERE id = ' . $db->quote((int) $this->serverId));
$db->close();
if (!$uploadServerDetails)
{
return false;
}
return $uploadServerDetails;
}
return false;
}
提前感谢您的宝贵时间。
说明:
我没有任何文件的直接链接。up函数是生成链接,远程服务器上的文件名不同(Handel由上述函数)。