首页 > 解决方案 > 将另一个 URL 添加到下载脚本中

问题描述

我有一个视频流媒体网站,它还允许用户下载视频。这运行在一个下载脚本上,该脚本在服务器上加载视频 ID 并启动直接文件传输。

现在我想在它前面放另一个 URL,而不是直接传输文件。我试图制定一个 mod_rewrite 规则,但它会导致循环并永远重定向到该 URL。它是一种允许货币化的链接缩短服务。

那么,我该如何归档呢?是否可以使用 mod_rewrite 规则对其进行归档?

尝试 mod_rewrite 规则,意识到我无法更改 PHP 中的 base_url 变量,因为它再也找不到文件了。

$vid   = intval($_GET['id']);
$label = intval($_GET['label']);

$sql = "SELECT * FROM video WHERE VID = ".$vid." LIMIT 1";
$rs = $conn->execute($sql); 
$formats = $rs->fields['formats'];
$server  = $rs->fields['server'];
$formats_arr = explode(',', $formats);

if ($server != '') {
    $sql = "SELECT * FROM video v, servers s WHERE v.VID = ".$vid." AND v.server = s.video_url LIMIT 1";
    $rs  = $conn->execute($sql); 
    $video_root = $rs->fields['video_url']; 
}
if (!$video_root) {
    $video_root = $config['BASE_DIR']."/media/videos";
}

foreach ($formats_arr as $format) {
    $f = explode('.', $format);
    if ($label == $f[1]) {
        if ($f[0] >= 481) {
            $condition = $new_permisions['hd_downloads'];
        } else {
            $condition = $new_permisions['sd_downloads'];           
        }
        $file = $video_root.'/h264/'.$vid.'_'.$f[1].'.'.$f[2];
        $file_name = $vid.'_'.$f[1].'.'.$f[2];
        break;
    }

}

if ($condition == 1) {
    ini_set('memory_limit', '-1');
    if (!$server) {
        if (file_exists($file) && is_file($file) && is_readable($file)) {
            $conn->execute("UPDATE video SET download_num = download_num+1 WHERE VID = ".$vid." LIMIT 1");
            @ob_end_clean();
            if(ini_get('zlib.output_compression')) {
                ini_set('zlib.output_compression', 'Off');
            }       
            header('Content-Type: application/force-download');
            header('Content-Disposition: attachment; filename="'.basename($file).'"');
            header('Content-Transfer-Encoding: binary');
            header('Accept-Ranges: bytes');
            header('Cache-control: private');
            header('Pragma: private');
            header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
            header('Content-Length: ' .filesize($file));             
            readfile($file);
            exit();
        } else {
            VRedirect::go($config['BASE_URL']. '/error');
        }
    } else {
            $conn->execute("UPDATE video SET download_num = download_num+1 WHERE VID = ".$vid." LIMIT 1");
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Content-Transfer-Encoding: chunked'); 
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');

            $stream = fopen('php://output', 'w');

            $ch = curl_init($file);
            curl_setopt($ch, CURLOPT_READFUNCTION, function($ch, $fd, $length) use ($stream) {
                return fwrite($stream, fread($fd, $length));
            });

下载按钮应该打开 URL/download.php,前面有我的链接缩短 URL,因此它将加载并重定向到文件下载 URL。

标签: phpmod-rewrite

解决方案


推荐阅读