【问题标题】:How can i have access to stored files on my different server via php header?如何通过 php 标头访问不同服务器上存储的文件?
【发布时间】:2018-04-10 17:12:36
【问题描述】:

有没有办法通过存储在原始域名(example.com/dl.php?id=12)中的 dl.php 文件下载子域(dl1.example.com)中存储的文件

在这种情况下,需要下载的文件位于 dl1(来自不同服务器的子域,并通过在 cpanel 上添加 DNS 记录选项连接)

dl.php 使用 php 标头为客户端创建链接

从 db ($path) 获取的 URL 并用作以下函数的输入:

downloadFile($path,$file_name, 1024, false);

$path 变量包含以下变量:

$path="http://dl1.example.com/Lamp%20with%20phpmyadmin.mp4";

我的主域名上存储的文件没有任何问题,当我想使用子域制作可下载链接时出现问题

我所有的代码:

/* List of File Types */
function fileTypes($extension){
$fileTypes['swf'] = 'application/x-shockwave-flash';
$fileTypes['pdf'] = 'application/pdf';
$fileTypes['exe'] = 'application/octet-stream';
$fileTypes['zip'] = 'application/zip';
$fileTypes['doc'] = 'application/msword';
$fileTypes['xls'] = 'application/vnd.ms-excel';
$fileTypes['ppt'] = 'application/vnd.ms-powerpoint';
$fileTypes['gif'] = 'image/gif';
$fileTypes['png'] = 'image/png';
$fileTypes['jpeg'] = 'image/jpg';
$fileTypes['jpg'] = 'image/jpg';
$fileTypes['rar'] = 'application/rar';

$fileTypes['ra'] = 'audio/x-pn-realaudio';
$fileTypes['ram'] = 'audio/x-pn-realaudio';
$fileTypes['ogg'] = 'audio/x-pn-realaudio';

$fileTypes['wav'] = 'video/x-msvideo';
$fileTypes['wmv'] = 'video/x-msvideo';
$fileTypes['avi'] = 'video/x-msvideo';
$fileTypes['asf'] = 'video/x-msvideo';
$fileTypes['divx'] = 'video/x-msvideo';

$fileTypes['mp3'] = 'audio/mpeg';
$fileTypes['mp4'] = 'audio/mpeg';
$fileTypes['mpeg'] = 'video/mpeg';
$fileTypes['mpg'] = 'video/mpeg';
$fileTypes['mpe'] = 'video/mpeg';
$fileTypes['mov'] = 'video/quicktime';
$fileTypes['swf'] = 'video/quicktime';
$fileTypes['3gp'] = 'video/quicktime';
$fileTypes['m4a'] = 'video/quicktime';
$fileTypes['aac'] = 'video/quicktime';
$fileTypes['m3u'] = 'video/quicktime';
return $fileTypes[$extention];
};

/*
Parameters: downloadFile(File Location, File Name,
max speed, is streaming
If streaming - videos will show as videos, images as images
instead of download prompt
*/
function downloadFile($fileLocation, $fileName, $maxSpeed = 100,     $doStream = false) {
if (connection_status() != 0)
    return(false);
//    in some old versions this can be pereferable to get extention
//    $extension = strtolower(end(explode('.', $fileName)));
$extension = pathinfo($fileName, PATHINFO_EXTENSION);

$contentType = fileTypes($extension);
header("Cache-Control: public");
header("Content-Transfer-Encoding: binary\n");
header('Content-Type: $contentType');

$contentDisposition = 'attachment';

if ($doStream == true) {
    /* extensions to stream */
    $array_listen = array('mp3', 'm3u', 'm4a', 'mid', 'ogg', 'ra', 'ram', 'wm',
        'wav', 'wma', 'aac', '3gp', 'avi', 'mov', 'mp4', 'mpeg', 'mpg', 'swf', 'wmv', 'divx', 'asf');
    if (in_array($extension, $array_listen)) {
        $contentDisposition = 'inline';
    }
}

if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
    $fileName = preg_replace('/\./', '%2e', $fileName, substr_count($fileName, '.') - 1);
    header("Content-Disposition: $contentDisposition;
        filename=\"$fileName\"");
} else {
    header("Content-Disposition: $contentDisposition;
        filename=\"$fileName\"");
}

header("Accept-Ranges: bytes");
$range = 0;
$size = filesize($fileLocation);

if (isset($_SERVER['HTTP_RANGE'])) {
    list($a, $range) = explode("=", $_SERVER['HTTP_RANGE']);
    str_replace($range, "-", $range);
    $size2 = $size - 1;
    $new_length = $size - $range;
    header("HTTP/1.1 206 Partial Content");
    header("Content-Length: $new_length");
    header("Content-Range: bytes $range$size2/$size");
} else {
    $size2 = $size - 1;
    header("Content-Range: bytes 0-$size2/$size");
    header("Content-Length: " . $size);
}

if ($size == 0) {
    die('Zero byte file! Aborting download');
}
set_magic_quotes_runtime(0);
$fp = fopen("$fileLocation", "rb");

fseek($fp, $range);

while (!feof($fp) and ( connection_status() == 0)) {
    set_time_limit(0);
    print(fread($fp, 1024 * $maxSpeed));
    flush();
    ob_flush();
    sleep(1);
}
fclose($fp);

return((connection_status() == 0) and ! connection_aborted());
}

/* Implementation */
// downloadFile('path_to_file/1.mp3', '1.mp3', 1024, false);


if(isset($_GET['i']) && is_numeric($_GET['i'])){
$file_id=input_security($_GET['i']);
$linkque=mysqli_query($connection,"SELECT * FROM download_links WHERE dl_id='".$file_id."'");
$checkexist=mysqli_num_rows($linkque);
$f_row=mysqli_fetch_array($linkque);
    if($checkexist==0){
        echo 'Invalid file id';
    }else if($checkexist>=1 && $f_row['payment_model']=="free"){
    /* Function: download with resume/speed/stream options */
        $path=$f_row['url'];
        $sep_parts = pathinfo($path);
        $file_name  = $sep_parts['basename'];
        $file_ext   = $sep_parts['extension'];
        if (file_exists($path)) {   
        downloadFile($path,$file_name, 1024, false);
        }else{
            echo "File is not exist or url is wrong.";
        }
}else if($checkexist>=1 && $f_row['payment_model']=="nofree"){
    if(isset($_SESSION['username'])){

    $client_id=input_security($_SESSION['username']);
    $client_id_query="SELECT userid FROM users WHERE emailuser='".$client_id."'";
    $session_query=mysqli_query($connection,$client_id_query);
    $clientidrow=mysqli_fetch_assoc($session_query);
    $client_id=$clientidrow['user_id'];
    $link_pid=$f_row['post_id'];
        $userorderque=mysqli_query($connection,"SELECT * FROM user_orders WHERE post_id='".$link_pid."' AND order_client_id='".$client_id."'");
        $checkorderexist=mysqli_num_rows($userorderque);
        $checktobuy_row=mysqli_fetch_array($userorderque);


        if($checkorderexist>=1 && $checktobuy_row['paid']==1){

        $path=$f_row['url'];
        $sep_parts = pathinfo($path);
        $file_name  = $sep_parts['basename'];
        $file_ext   = $sep_parts['extension'];
        if (file_exists($path)){
        //echo "thank you for purchase and support us. You can download now.";
            downloadFile($path,$file_name, 1024, false);
        }else{
            echo "File is not exist or url is wrong.";
        }
        }else if($checkorderexist>=1 && $checktobuy_row['paid']==0){
            echo "You Need to pay for download this file";
        }
    }else{
        echo "This file needs to login";
    }



}else{
    echo "You don't have permission to download this file please purchase this product and trying to download again";
    }
}else{
    header('Location: index.php');
}

如何从存储在我的域名上的脚本(我的意思是我的主 Web 服务器主机(例如.com)) ?

如果有任何帮助,我将不胜感激

【问题讨论】:

  • 不清楚你想做什么。
  • 我想分享产品链接,但仅供付费客户使用。

标签: php .htaccess dns header


【解决方案1】:

不是 100% 清楚您的通信流程如何,但根据您的描述,您只是处理跨域问题。您可以通过将此标头添加到您的服务器响应以供下载,从而允许在您的服务器上进行跨域调用:

header("Access-Control-Allow-Origin: *");

可以在这里找到使用不同环境的好例子:

https://enable-cors.org/

【讨论】:

  • header("Access-Control-Allow-Origin: *");没用,能给我看一个简单的例子吗?
  • 我想我们现在可以聊一聊这个。您的案例描述甚至没有说明跨域是否真的是您的问题。如果是这样,您需要自行查找针对您的特殊环境的某些解决方案,或者更新您的问题以提供更多详细信息
猜你喜欢
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
相关资源
最近更新 更多