使用php代码从URL下载图像? [英] Download image from URL using php code?

查看:113
本文介绍了使用php代码从URL下载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用php从网址下载图片(例如: https:// www.google.com/images/srpr/logo3w.png )然后保存吗?
这是我到目前为止提出的,它在'file_put_contents'函数中给出了一个错误。

How can I use php to download an image from URL (eg: https://www.google.com/images/srpr/logo3w.png) then save it? This is what I came up with so far, it gives me an error in 'file_put_contents' function.

<form method="post" action="">
<textarea name="text" cols="60" rows="10">
</textarea><br/>
<input type="submit" class="button" value="submit" />
</form>

<?php
    $img = "no image";
    if (isset($_POST['text']))
    {
    $content = file_get_contents($_POST['text']);
    $img_path = '/images/';
    file_put_contents($img_path, $content);    
    $img = "<img src=\"".$img_path."\"/>";
    }
    echo $img;
?>

它给了我错误:

[function.file-put-contents]:无法打开流:C:\ wamp \\\\ img.php中没有这样的文件或目录

/ images / 目录位于php文件的同一目录中并且是可写的。

The /images/ directory is located in the same directory of the php file and is writable.

推荐答案

您无法使用现有代码保存图像,因为您没有提供文件名。你需要做类似的事情:

You cannot save the image with your existing code because you do not provide a file name. You need to do something like:

$filenameIn  = $_POST['text'];
$filenameOut = __DIR__ . '/images/' . basename($_POST['text']);

$contentOrFalseOnFailure   = file_get_contents($filenameIn);
$byteCountOrFalseOnFailure = file_put_contents($filenameOut, $contentOrFalseOnFailure);

这篇关于使用php代码从URL下载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆