+
80
-

php curl下载视频文件大小为0怎么办?

今天想要php 的curl下载mp4文件,但是下载后的文件大小怎么是0kb?

<?php
$source = "https://repo.bfw.wiki/bfwrepo/video/modushanghai.mp4";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $source);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_SSLVERSION, 3);

$data = curl_exec ($ch);

$error = curl_error($ch);

curl_close ($ch);

$destination = "/data/wwwroot/default/Data/video.mp4";

$file = fopen($destination, "wb");

fwrite($file, $data);

fclose($file);

800_auto

网友回复

+
0
-

这个代码不对,改成下面的写法就可以了

<?php

$filename = "/data/wwwroot/default/Data/video.mp4"; //路径文件名
$file = "https://repo.bfw.wiki/bfwrepo/video/modushanghai.mp4";
download_img($file, $filename);

function download_img($url, $filename) {
    // $COOKIE_file = dirname(__FILE__).'/COOKIE.txt';
    $ch = curl_init ();
    curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt ($ch, CURLOPT_URL, $url);
    // curl_setopt($ch, CURLOPT_COOKIEFILE,$COOKIE_file);
    ob_start ();
    curl_exec ($ch);
    $return_content = ob_get_contents ();
    ob_end_clean ();

    $return_code = curl_getinfo ($ch, CURLINFO_HTTP_CODE);
    $fp = @fopen($filename, "a");
    fwrite($fp, $return_content);
    return true;
}

我知道答案,我要回答