Saving image from PHP URL
- riad
- 2009-04-07 06:50
- 9
I need to save an image from a PHP URL to my PC. Let's say I have a page, http://example.com/image.php
, holding a single "flower" image, nothing else. How can I save this image from the URL with a new name (using PHP)?
9 Answers
copy('http://example.com/image.php', 'local/folder/flower.jpg');
php save image from url to folder, Saving an Image from URL in PHP Sometimes, need to download an image from a particular URL and use it into the project. It’s easy to go to the page and use right click button and save the image. But what if you wanted to do it programmatically? 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
Halil Özgür
2015-07-08 14:22
$content = file_get_contents('http://example.com/image.php'); file_put_contents('/my/folder/flower.jpg', $content);
php get image from url and display it, after no one answered at this question Php Rss feed use img in CDATA -> content:encoded i try to do something else solving this problem how can i load an image from a given url directly php $url = ''; $image = base64_encode(file_get_contents($url)); ?> Then you can display it: <img src="data:image/x-icon;base64,<? = $image ?>">
soulmerge
2009-04-07 07:23
Here you go, the example saves the remote image to image.jpg.
function save_image($inPath,$outPath) { //Download images from remote server $in= fopen($inPath, "rb"); $out= fopen($outPath, "wb"); while ($chunk = fread($in,8192)) { fwrite($out, $chunk, 8192); } fclose($in); fclose($out); } save_image('http://www.someimagesite.com/img.jpg','image.jpg');
php script to download all images from url, 2: curl stops the download when the Content-Length http header's bytes has the main problem with php, if you are executing this code from a browser, but if you have a complete URL and a pattern, as I can see, you can
Sam152
2009-04-07 08:00
Vartec's answer with cURL didn't work for me. It did, with a slight improvement due to my specific problem.
e.g.,
When there is a redirect on the server (like when you are trying to save the facebook profile image) you will need following option set:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
The full solution becomes:
$ch = curl_init('http://example.com/image.php'); $fp = fopen('/my/folder/flower.gif', 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_exec($ch); curl_close($ch); fclose($fp);
php get all images from url, PHP Get All URL's of Images From String. Ask Question Asked 7 years, Use regex to get image URL in HTML/Js. 2. PHP Regex to get all image URLs on the page. php $url_image = $_GET['url']; $homepage = file_get_contents($url_image); preg_match_all("{<img\\s*(. *?) src=('. *?
stoefln
2017-05-23 11:54
I wasn't able to get any of the other solutions to work, but I was able to use wget:
$tempDir = '/download/file/here'; $finalDir = '/keep/file/here'; $imageUrl = 'http://www.example.com/image.jpg'; exec("cd $tempDir && wget --quiet $imageUrl"); if (!file_exists("$tempDir/image.jpg")) { throw new Exception('Failed while trying to download image'); } if (rename("$tempDir/image.jpg", "$finalDir/new-image-name.jpg") === false) { throw new Exception('Failed while trying to move image file from temp dir to final dir'); }
codeigniter save image from url, Less Searching. More Finding. Explore Millions of Exclusive Contents at iStock™. Single Image Starting at $12, Plans Starting at $29 and The Lowest Price in 4K Videos. I'm using CodeIgniter and I want to use this with the upload() method, but any other method will also be appreciated. share.
Andrew
2010-12-03 17:45
See file()
PHP Manual:
$url = 'http://mixednews.ru/wp-content/uploads/2011/10/0ed9320413f3ba172471860e77b15587.jpg'; $img = 'miki.png'; $file = file($url); $result = file_put_contents($img, $file)
save image from blob url in php, Save Image from URL using PHP The following code snippet helps you to copy an image file from remote URL and save in a folder using PHP. file_get_contents () – This function is used to read the image file from URL and return the content as a string. file_put_contents () – This function is used to write remote image data to a file. getContext("2d"); var img = new Image(); img.src = url; img.onload = function and one more element on the form to save the Data URL:
zloctb
2015-04-05 00:29
$img_file='http://www.somedomain.com/someimage.jpg' $img_file=file_get_contents($img_file); $file_loc=$_SERVER['DOCUMENT_ROOT'].'/some_dir/test.jpg'; $file_handler=fopen($file_loc,'w'); if(fwrite($file_handler,$img_file)==false){ echo 'error'; } fclose($file_handler);
php display image from file, Show image using file_get_contents. Ask Question Php display image from url into homepage-2. php get image from url. 2. Protect image directory. 1. Use PHP to php $file = 'your_images. jpg'; header('Content-Type: image/jpeg'); header('Content-Length: ' . filesize($file)); echo file_get_contents($file); ?> Small edit to @seengee answer: In order to work, you need curly braces around the variable, otherwise you'll get an error.
karl
2011-05-11 15:59
Create a folder named images located in the path you are planning to place the php script you are about to create. Make sure it has write rights for everybody or the scripts won't work ( it won't be able to upload the files into the directory).
php create image and save to file, I am creating an image via a php script using imagepng. This works fine and displays well on the website. even saving-as gives me a valid .png file The above example will output something similar to: Output of example : Outputting a JPEG image. Example #2 Saving a JPEG image to a file. <?php // Create a
Ibrahim
2011-01-06 12:52
If you have
allow_url_fopen
set totrue
:Else use cURL:
Saving image from PHP URL, Use file_put_contents() function to write a string to a file that takes two arguments. Use file_get_contents() function to read a file into a string.
vartec
2010-12-03 00:19