17

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;
?>

It gives me the error:

[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.

3
  • 4 answers in 1min!! Hope your problems are quickly solved. Commented Mar 21, 2012 at 9:12
  • you have never accepted any answer? Kindly upvote and accept the answers that helped you. Commented Mar 21, 2012 at 9:14
  • @Rohit ok found out how to accept :) Commented Mar 21, 2012 at 9:24

5 Answers 5

34

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);
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for replying. I already used file_get_contents in the code. It works fine but I cant save the image.
@Sarah: My bad, I misread the code. Edited the answer in the meanwhile.
This is the error I get: [function.file-put-contents]: failed to open stream: No such file or directory in C:\wamp\www\img.php on line 14
@Sarah: Does the directory /images/ exist? Is it writable by your web server? Basic debugging.
@Sarah: Then /images/ does not exist, but ./images/ exists. I edited the answer to match.
|
1

file_put_contents() requires first parameter to be file name not the path.

Comments

1

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 :

example

$img_path="home/downloads/my_images";
file_put_contents($img_path."/flower.jpg");

2 Comments

Thanks for replying! So I need to specify the image name in the code?
Yes, you could use your $_POST['text'] but be aware of bad contents. You need to sanitize your $_POST and then work on it to get the name of the image. Look at basename it can maybe helps.
0
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

Comments

0

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 .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.