PHP copy image from url to server and echo the final url
You should use file_get_contents
or curl to download the file. Also note that $newUrl
inside your function is local and this assignment doesn't alter the value of global $newUrl
variable, so you can't see it outside your function. And the statement $newUrl;
in 3rd line doesn't make any sense.
Copy Image from Remote Server Over HTTP
If you have PHP5 and the HTTP stream wrapper enabled on your server, it's incredibly simple to copy it to a local file:
copy('http://somedomain.com/file.jpeg', '/tmp/file.jpeg');
This will take care of any pipelining etc. that's needed. If you need to provide some HTTP parameters there is a third 'stream context' parameter you can provide.
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';
}
?>
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);
Copy img from url to server: no such file or directory
function download_image($url,$destination_path = '')
{
// CHECKS IF CURL DOES EXISTS. SOMETIMES WEB HOSTING DISABLES FILE GET CONTENTS
if (function_exists('curl_version'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
} else
{
$content = file_get_contents($url);
}
// CHECKS IF DIRECTORY DOESNT EXISTS AND DESTINATION PATH IS NOT EMPTY
if(!file_exists($destination_path) && $destination_path != ''){
mkdir($destination_path, 0755, true);
}
// ATTEMPT TO CREATE THE FILE
$fp = fopen($destination_path.'/'.date('YmdHis').".jpg", "a+");
fwrite($fp, $content);
fclose($fp);
}
download_image('http://davidwalsh.name/wp-content/themes/jack/images/treehouse-1.png','images');
Copying Images from URl list to my server all at once by php
I would create a script that reads your html file line per line.
You can do that using fopen
and fgets
.
fopen("path/to/some/file", "r");
while ( ( $line = fgets( $handle ) ) !== false )
{
// do somehting with $line
}
This way the file gets not simply parsed into memory, so you don't have to worry about size
Then after parsing every line I would write down a lock file containing the current line number / index. So if your script crashes and you restart it the iteration simply skips every line until it's current index is higher than the index from the lock file.
the script
It might work but, in the end should not simply copy paste everything. But i hope it helps you finding your solution.
#!/usr/bin/env php
<?php
// I DID NOT TEST THIS!
// but it should work.
$handle = fopen("path/to/the/html/file/containing/the/urls.html", "r");
$storage = "path/where/you/want/your/images/";
$lockFile = __DIR__.'/index.lock';
$index = 0;
// get the lock index
if ( !file_exists( $lockFile ) )
{
file_put_contents( $lockFile, 0 );
}
// load the current index
$start = file_get_contents( $lockFile );
if ( $handle )
{
// line by line step by step
while ( ( $line = fgets( $handle ) ) !== false )
{
// update the
$index++;
if ( $start > $index )
{
continue;
}
// match the url from the element
preg_match( '/<a href="(.+)">/', $line, $url ); $url = $url[1];
$file = basename( $url );
// check if the file already exists
if ( !file_exists( $storage.$file )) //edited
{
file_put_contents( $storage.$file, file_get_contents( $url ) );
}
// update the lock file
file_put_contents( $lockFile, $index );
}
fclose($handle);
}
else
{
throw new Exception( 'Could not open file.' );
}
Related Topics
Block Direct Access to a File Over Http But Allow PHP Script Access
How to Update Stock Quantity Using MySQL in PHP
Laravel 5.5 Http Error 500 Unable to Handle This Request
Object of Class Mysqli_Result Could Not Be Converted to String
Redirect That Same Page from Where Id Is Coming
Trim Country Code from Phone Number in PHP
Dompdf Remote Image Is Not Displaying in Pdf
How to Get the Path to the Laravel Storage Folder
Laravel: Pdoexception: Could Not Find Driver
In Laravel How to Get Data in One Query from 3 Tables
How to Redirect to the Same Page in PHP
Getting Check Box Values as Checked from Database Codeigniter
Selected Value Get from Db into Dropdown Select Box Option Using PHP MySQL Error
Php 7 With Phpmyadmin Gives Lots of Deprecation Notices
How to Add Whatsapp Share Button to PHP Website