Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save an image from a URL then save it into a directory php [duplicate]

Tags:

php

Possible Duplicate:
save image from php url using php

How can i use php to save/grab an image from another domain(not on my domain/server) and save it in a directory of my site.

The URL of the image for example , will be :

http://anothersite/images/goods.jpg

How can i use PHP to grab "good.jpg" ,and save it in my directory which is www.mysite.com/directory/

I hope someone could guide me. Thanks!

like image 992
sm21guy Avatar asked Dec 05 '11 07:12

sm21guy


People also ask

How do I copy an image from one directory to another in PHP?

The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure.

How can I download image from API in PHP?

file_put_contents ( $img , file_get_contents ( $url )); echo "File downloaded!" File downloaded! Note: It save the image to the server with given name logo.

How do I save a directory in PHP?

php $target_Path = "images/"; $target_Path = $target_Path. basename( $_FILES['userFile']['name'] ); move_uploaded_file( $_FILES['userFile']['tmp_name'], $target_Path ); ?> when the file(image) is saved at the specified path... WHAT if i want to save the file with some desired name....


1 Answers

You should be able to use file_get_contents for this one. In order to use an URL with file_get_contents make sure allow_url_fopen is enabled in you php.ini file.

define('DIRECTORY', '/home/user/uploads');

$content = file_get_contents('http://anothersite/images/goods.jpg');
file_put_contents(DIRECTORY . '/image.jpg', $content);

Make sure that you have write permission to the directory where you want to store the image; to make the folder writable you could do this:

chmod +w /home/users/uploads

References

  1. file_get_contents
  2. allow_url_fopen
  3. chmod command
like image 142
Cyclonecode Avatar answered Oct 12 '22 23:10

Cyclonecode