php youtube视频不下载在移动的

ymdaylpp  于 2023-03-21  发布在  PHP
关注(0)|答案(1)|浏览(149)

在php中,youtube视频在桌面上下载很好,但在移动的设备上其downloaidng与.html扩展名.此外,当下载视频,它不显示文件大小,我如何修复它请帮助...

<?php
$downloadURL = urldecode($_GET['link']);
$type = urldecode($_GET['type']); // mp4, 3gp , mp3
$title = urldecode($_GET['title']);
$fileName = $title.'.'.$type;

if (!empty($downloadURL) && substr($downloadURL, 0, 8) === 'https://') {
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=".basename($fileName));
    header("Content-Transfer-Encoding: binary");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Pragma: public");
    ob_clean();
    flush();
    readfile($downloadURL);
}
?>

帮助我下载视频在移动的以及在桌面上,也应该显示文件大小时下载.我如何才能解决这两个问题?

23c0lvtd

23c0lvtd1#

也许移动的重命名下载视频格式。请使用下面的代码。它可能会对你有帮助。
我添加了多个移动的用户代理。

<?php
$downloadURL = urldecode($_GET['link']);
$type = urldecode($_GET['type']); // mp4, 3gp , mp3
$title = urldecode($_GET['title']);
$fileName = $title.'.'.$type;

if (!empty($downloadURL) && substr($downloadURL, 0, 8) === 'https://') {
    $ch = curl_init($downloadURL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);
    $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    curl_close($ch);

    if(strstr($_SERVER['HTTP_USER_AGENT'],'Android') ||
        strstr($_SERVER['HTTP_USER_AGENT'],'webOS') ||
        strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') ||
        strstr($_SERVER['HTTP_USER_AGENT'],'iPad') ||
        strstr($_SERVER['HTTP_USER_AGENT'],'iPod') ||
        strstr($_SERVER['HTTP_USER_AGENT'],'Windows Phone')){
            $fileName = $title.'.'.$type;
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.$fileName.'"');
        }else{
            header("Content-Type: application/octet-stream");
            header("Content-Disposition: attachment; filename=".basename($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: ' . $size);
    ob_clean();
    flush();
    readfile($downloadURL);
}
?>

相关问题