php 使用php代码从URL下载图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9801471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download image from URL using php code?
提问by Sarah
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.
如何使用 php 从 URL 下载图像(例如:https://www.google.com/images/srpr/logo3w.png)然后保存它?到目前为止,这是我想出的,它给了我'file_put_contents'函数的错误。
<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;
?>
It gives me the error:
它给了我错误:
[function.file-put-contents]: failed to open stream: No such file or directory in C:\wamp\www\img.php
[function.file-put-contents]: failed to open stream: No such file or directory in C:\wamp\www\img.php
The /images/
directory is located in the same directory of the php file and is writable.
该/images/
目录位于php文件的同一目录下,是可写的。
回答by Jon
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);
回答by NeeL
What is your error ? But you have the right way to do what you want to do.
你的错误是什么?但是你有正确的方法去做你想做的事。
Just take care in your code, I can see file_put_contents($img_path,
but $img_path
is a path to a folder.
You need to write something like :
请注意您的代码,我可以看到file_put_contents($img_path,
但它$img_path
是文件夹的路径。你需要写一些类似的东西:
example
例子
$img_path="home/downloads/my_images";
file_put_contents($img_path."/flower.jpg");
回答by Rohit Vipin Mathews
file_put_contents($img_path, $content);
To what are you putting to?? i think you have made a mistake with get
and put
. or missed to specify the full path including the file name and extension
你要干什么??我认为你在get
和 上犯了一个错误put
。或错过指定完整路径,包括文件名和扩展名
回答by Janith Chinthana
I added one condition to accepted answer to check the image type as follows,
我在接受的答案中添加了一个条件来检查图像类型,如下所示,
if (exif_imagetype($_POST['text']) == IMAGETYPE_PNG) {
$filename = './images/'.basename($_POST['text']);
file_put_contents($filename, $content);
}
from above example I show how to check png images before download the files and for more content types you can find here.
从上面的示例中,我展示了如何在下载文件之前检查 png 图像以及更多内容类型,您可以在此处找到。
回答by heyanshukla
file_put_contents()
requires first parameter to be file name not the path.
file_put_contents()
要求第一个参数是文件名而不是路径。