将 facebook 图片下载到本地服务器

发布于 2024-12-16 20:20:43 字数 392 浏览 4 评论 0原文

我正在使用

function save_image($inPath,$outPath){
    //Download images from remote server
    $in=    fopen($inPath, "rb");
    $out=   fopen($outPath, "wb");
    while ($chunk = fread($in,8192))
    {
        fwrite($out, $chunk, 8192);
    }
    fclose($in);
    fclose($out);
}

此功能,工作正常,但如果我打开图像,它显示文件为空。

我也尝试了 file_get_content() 函数。但仍然遇到同样的问题。

i am using

function save_image($inPath,$outPath){
    //Download images from remote server
    $in=    fopen($inPath, "rb");
    $out=   fopen($outPath, "wb");
    while ($chunk = fread($in,8192))
    {
        fwrite($out, $chunk, 8192);
    }
    fclose($in);
    fclose($out);
}

This function working perfectly , but if i open the image it show file is empty.

i also tried file_get_content() function .But still having the same problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念三年u 2024-12-23 20:20:46

我总是更喜欢使用curl来做这种事情。

function saveImage($url, $savePath) 
{

    $ch = curl_init($url);
    $fp = fopen($savePath, 'wb');

    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    $result = curl_exec($ch);

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

    return $result;

}

如果您没有安装curl,请查看官方PHP curl 安装说明 ,安装Windows的curl非常简单。

I always prefer to use curl to do this kind of thing.

function saveImage($url, $savePath) 
{

    $ch = curl_init($url);
    $fp = fopen($savePath, 'wb');

    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    $result = curl_exec($ch);

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

    return $result;

}

If you don't have curl installed check the official PHP curl installation instructions, installing curl of Windows is pretty easy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文