如何下载远程图像文件?

时间:2018-08-20 09:18:23

标签: php azure download

我想从我的azure blob存储区下载我的图像和pdf等文件。我尝试使用此代码给我空白文件。有人可以帮忙吗?这是我的代码。我的网址就像。https://xxxx.blob.core.windows.net/68-documents/15347493127361.png。我是从html通过标签获取的。

    set_time_limit(0);

    $url = $this->input->get("url");
    $file = basename($url);

    $fp = fopen($file, 'w');

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);

    $data = curl_exec($ch);

    curl_close($ch);
    fclose($fp);

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;

我更改了这样的代码仍然无法正常工作

set_time_limit(0);
$url = 'https://xxxx.blob.core.windows.net/68-documents/15347493127361.png';
$file = basename($url);
$data = file_get_contents($url);
header("Content-Disposition: attachment; filename=\"" . basename($file) . "\"");
header("Content-Type: application/octet-stream");
header("Content-Length: " . strlen($data));
header("Connection: close");
?>

0 个答案:

没有答案