1

I need to create a .PNG file from a file that I am dowloading by curl_setopt().
The .PNG will be created, only it can't be read anymore.
So my thougts are, the $rawdata does not have the right content.

// 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.';
MrLine
  • 638
  • 7
  • 25

1 Answers1

-1

Try file_put_contents($file_name, $rawdata) instead of

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

or base64_encode($rawdata) in both variants

Slava
  • 878
  • 5
  • 8