Saving image from PHP URL
If you have allow_url_fopen
set to true
:
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
Else use cURL:
$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_exec($ch);
curl_close($ch);
fclose($fp);
Save image from url to local System in php
The problem is that the "images" you are using in the file_put_contents is not getting the path that where to save the file. try to make the images folder along with php file which having this code and see its working.
<?php
$imageUrl = 'https://cwsimages.ingramtest.com/cdsImages/imageloader?id=pBbFysOWRLJoSy4l4lbc+yLblU6JMuhKpze3XsQNO+njA3/XYRYbXSEYYsSqKXoiGD07duAyOSVXNUVLvxDqlMx15WtRQWJn3xC/twmM2s62tw+XgriCmEXBHawun03pQLBHXLuEQhNmCb8MC3ZMNH7pe5O76s18u/mgplf8YtU=';
@$rawImage = file_get_contents($imageUrl);
if($rawImage)
{
file_put_contents("images/".'dummy1.png',$rawImage);
echo 'Image Saved';
}
else
{
echo 'Error Occured';
}
?>
Download image from URL using php code?
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);
How to save image from URL in server folder using php or javascript?
You can use file_get_contents($url)
in order to retrieve the contents of any remote file if you have allow_url_fopen
enabled in your PHP configuration.
Once you download the image into memory, then you can save it with
file_put_contents($img, $imgcontent)
Example might look like this:
$url = 'https://www.google.com/abc/files/AwAh20isBECxwscp4JiT';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
If you don't allow remote URLs to be opened by fopen()
, then you have the option of using the cURL
functionality to grab files remotely.
$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_exec($ch);
curl_close($ch);
fclose($fp);
Duplicate of: Saving image from PHP URL
save an image from a URL then save it into a directory php
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
- file_get_contents
- allow_url_fopen
- chmod command
Saving a remote image programmatically with PHP
You can use PHP's copy function to copy remote files to a location on your server:
copy("https://example.com/some_image.jpg", "/path/to/file.jpg");
http://php.net/manual/en/function.copy.php
Save images from url strings
//SORT THE ARRAYS BY NAME (See above)
You should use usort() (i'm assuming PHP 5.3+ here):
usort($your_array, function ($elem1, $elem2) {
return strcmp($elem1['title'], $elem2['title']);
});
//DETECT IMAGES FROM THIS STRING-ARRAY
For each string in array, veirfy if it contains ".jpg" using strrpos
strrpos($foo, '.jpg', -4)
//SAVE EACH IMAGE ON A REMOTE SERVER IN A SPECIFIC FOLDER (004, 007)
Use copy function:
copy('http://www.google.co.in/img/product/004/big/004-30.jpg', '/tmp-folder/004-30.jpeg');
Related Topics
Difference Between Require, Include, Require_Once and Include_Once
Apache Shows PHP Code Instead of Executing It
PHP - Floating Number Precision
Should I Manually Check For Errors When Calling "MySQLi_Stmt_Prepare"
Simplest Way to Detect a Mobile Device in PHP
Form Submit With Ajax Passing Form Data to PHP Without Page Refresh
PHP $_Post Array Empty Upon Form Submission
How to Remove Multiple Utf-8 Bom Sequences
How to [Recursively] Zip a Directory in PHP
How to Get the Jquery $.Ajax Error Response Text
PHP: Return All Dates Between Two Dates in an Array
PHP Does Not Display Error Messages
How to Maintain a "Version For the Server" - With Only Config Files Changed, in Git
Creating the Singleton Design Pattern in PHP5
How to Validate an Email in PHP