Php 通过curl_setopt()获取文件并将其写入.PNG格式

Php 通过curl_setopt()获取文件并将其写入.PNG格式,php,curl,fwrite,Php,Curl,Fwrite,我需要从我正在通过curl\u setopt()加载的文件中创建一个.PNG文件 将创建.PNG,但不能再读取。 因此,我的想法是,$rawdata没有正确的内容 // Function to download a file (needed in one of the programming missions) function htsGetFile($url, $filename) { $cookiefile = "cookie.txt"; $ch = curl_init()

我需要从我正在通过
curl\u setopt()
加载的文件中创建一个
.PNG
文件
将创建
.PNG
,但不能再读取。
因此,我的想法是,
$rawdata
没有正确的内容

// Function to download a file (needed in one of the programming missions)
function htsGetFile($url, $filename) {
    $cookiefile = "cookie.txt";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0');

    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); // Cookiejar on creation, cookiefile on use

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // We need tot get the data back.
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //Will not check the ssl sertification.

    $rawdata = curl_exec($ch);

    if(file_exists($filename)){
        unlink($filename);
    }

    $fp = fopen($filename, 'wb');
    fwrite($fp, $rawdata);
    fclose($fp);

    if(curl_errno($ch)) {
        return false;
    } else {
        return $rawdata;
    }
    curl_close ($ch);
}




$username = "*******";
$password = "*******";

$url = "https://www.domain/path/";
$file = "download.png";    

if (htsLogin($username, $password))
    if (htsGetHTML($url) !== false)
        if (htsGetFile($url, $file) !== false)
            $img = htsGetFile($url, $file);
        else
            echo 'Getting file went wrong.';
    else
        echo 'Getting page went wrong.';
else
    echo 'Can\'t login.';

尝试
file\u put\u contents($file\u name,$rawdata)
而不是

$fp = fopen($filename, 'wb');
fwrite($fp, $rawdata);
fclose($fp);

或者
base64\u encode($rawdata)
在这两种变体中

检查
png
的内容,在一个太像记事本的记事本++中查看发生了什么。@justonUndermillion,我做了,我得到了一个磨损的文本,我想他们称之为二进制代码。但我看到了一些东西,比如边框背景是黑色的,里面有一个带有nulladd
curl_setopt($ch,CURLOPT_HEADER,0)的文本请看这里,这两种变体中的
或base64_encode($rawdata)是什么意思?
如果文件将被破坏,请尝试
文件内容($file_name,base64_encode($rawdata))