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.

Sarah
  • 459
  • 3
  • 11
  • 20

5 Answers5

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);
hakre
  • 193,403
  • 52
  • 435
  • 836
Jon
  • 428,835
  • 81
  • 738
  • 806
  • Thanks for replying. I already used `file_get_contents` in the code. It works fine but I cant save the image. – Sarah Mar 21 '12 at 09:11
  • @Sarah: My bad, I misread the code. Edited the answer in the meanwhile. – Jon Mar 21 '12 at 09:12
  • 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 Mar 21 '12 at 09:30
  • @Sarah: Does the directory `/images/` exist? Is it writable by your web server? Basic debugging. – Jon Mar 21 '12 at 09:31
  • it exists in the same directory where the php file is, and it is writable. – Sarah Mar 21 '12 at 09:34
  • 1
    @Sarah: Then `/images/` does not exist, but `./images/` exists. I edited the answer to match. – Jon Mar 21 '12 at 09:42
  • Warning to anyone coming across this later! don't just accept a file and assume its an image - it could be a php script, and then all the visitor has to do is run the PHP just uploaded and own your server. – Snapey Apr 16 '14 at 17:57
1

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

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
heyanshukla
  • 701
  • 8
  • 17
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");
NeeL
  • 720
  • 6
  • 20
  • Thanks for replying! So I need to specify the image name in the code? – Sarah Mar 21 '12 at 09:12
  • 1
    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](http://ca3.php.net/manual/fr/function.basename.php) it can maybe helps. – NeeL Mar 21 '12 at 09:17
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 .

Janith Chinthana
  • 3,792
  • 2
  • 27
  • 54
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

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112