83

I am looking for a simple way to import/copy images from remote server to a local folder using PHP. I have no FTP access to the server, but all remote images can be accessed via HTTP (i.e. http://www.mydomain.com/myimage.jpg).

Example use: A user wishes to add an image to his profile. The image already exists on the web and the user provides with a direct URL. I do not wish to hotlink the image but to import and serve from my domain.

GiladG
  • 1,123
  • 1
  • 8
  • 7

11 Answers11

157

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.

Ciaran McNulty
  • 18,698
  • 6
  • 32
  • 40
  • 33
    Remember allow_url_fopen has to be = on in php.ini. – mauro.dec Aug 20 '09 at 13:37
  • 3
    The allow_url_fopen comment just solved over an hour of head-scratching. Thanks, @mauro.dec! – Michael Stalker Jun 30 '12 at 19:36
  • 2
    what are doning with URLs like this `http://desmond.imageshack.us/Himg100/scaled.php?server=100&filename=desertoh.jpg&res=crop` ??? – Yuseferi Jul 14 '12 at 09:32
  • Thanks for this answer - I think im almost there! When you say '/tmp/file.jpg' - what is that relative to? The ROOT of the web server... (eg: /var/www/vhosts etc) or the root of the website: /httpdocs/... ?? Cant see the files appearing, and I have url-fopen set to ON. !! – itsricky Jan 16 '13 at 23:48
  • @itsricky `/tmp/my.file` - leading slash always refers to the _root_ of the file system (_server in your case_) regardless "environment" you are using. – Paul T. Rawkeen Jul 30 '13 at 07:22
  • 1
    Don't use https, just http – Edwin Thomas Jan 28 '15 at 05:10
  • I think it's important to remember security when doing this - you're going to want to be careful about what files you pull onto your server, and if they're in a path where things like executables or PHP is runnable. – Scott Flack May 25 '15 at 01:00
32

if nothing works, use this quick solution

$imageString = file_get_contents("http://example.com/image.jpg");
$save = file_put_contents('Image/saveto/image.jpg',$imageString);
dazzafact
  • 2,570
  • 3
  • 30
  • 49
10

PHP has a built-in function file_get_contents(), which reads the content of a file into a string.

<?php
//Get the file
$content = file_get_contents("http://example.com/image.jpg");

//Store in the filesystem. $fp = fopen("/location/to/save/image.jpg", "w"); fwrite($fp, $content); fclose($fp); ?> If you wish to store the file in a database, simply use the $content variable and don't save the file to disk.

  • 2
    using curl is a good method 'cause some server guys not support the file_get_contents for security reasons – Duke Feb 20 '12 at 09:10
  • 1
    Nice tip for small files, but for larger files php memory will explode :( curl is also no good choice, because its no part of standard php – Radon8472 May 25 '14 at 00:42
8

You've got about these four possibilities:

  • Remote files. This needs allow_url_fopen to be enabled in php.ini, but it's the easiest method.

  • Alternatively you could use cURL if your PHP installation supports it. There's even an example.

  • And if you really want to do it manually use the HTTP module.

  • Don't even try to use sockets directly.

Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
  • Why the "Don't even try to use sockets directly."? (Which is what I am using, https://gist.github.com/0893a7ec908ff508aac6) – Gajus Nov 16 '12 at 00:45
  • @Gajus: You're not using sockets directly in that snippet. You're using the capability of php to open remote files. – Georg Schölly Nov 16 '12 at 09:10
4

Here's the most basic way:

$url = "http://other-site/image.png";
$dir = "/my/local/dir/";

$rfile = fopen($url, "r");
$lfile = fopen($dir . basename($url), "w");

while(!feof($url)) fwrite($lfile, fread($rfile, 1), 1);

fclose($rfile);
fclose($lfile);

But if you're doing lots and lots of this (or your host blocks file access to remote systems), consider using CURL, which is more efficient, mildly faster and available on more shared hosts.

You can also spoof the user agent to look like a desktop rather than a bot!

$url = "http://other-site/image.png";
$dir = "/my/local/dir/";
$lfile = fopen($dir . basename($url), "w");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
curl_setopt($ch, CURLOPT_FILE, $lfile);

fclose($lfile);
curl_close($ch);

With both instances, you might want to pass it through GD to make sure it really is an image.

Oli
  • 235,628
  • 64
  • 220
  • 299
  • 1
    For checking the image with GD, consider running this right after you save the file: http://www.psoug.org/snippet/Check_if_an_image_exists_GD_364.htm - if it comes back false, delete the file immediately. – Oli May 26 '09 at 07:54
  • 5
    You missed `curl_exec($ch)` before `curl_close($ch)`, and `fclose($ch)` should be placed in the end rather than before `curl_close($ch)`, that means close the file before nothing has been written to the file, therefore you'll only get an empty file. – O.O Dec 06 '10 at 09:54
2

make folder and name it foe example download open note pad and insert this code

only change http://www.google.com/aa.zip to your file and save it to m.php for example

chamod the php file to 666 and the folder download to 777

<?php
define('BUFSIZ', 4095);
$url = 'http://www.google.com/aa.zip';
$rfile = fopen($url, 'r');
$lfile = fopen(basename($url), 'w');
while(!feof($rfile))
fwrite($lfile, fread($rfile, BUFSIZ), BUFSIZ);
fclose($rfile);
fclose($lfile);
?>

finally from your browser enter to these URL http://www.example.com/download/m.php

you will see in download folder the file download from other server

thanks

2

It's extremely simple using file_get_contents. Just provide the url as the first parameter.

PatrikAkerstrand
  • 45,315
  • 11
  • 79
  • 94
0

For those who need to preserve the original filename and extension

$origin = 'http://example.com/image.jpg';

$filename = pathinfo($origin, PATHINFO_FILENAME);
$ext = pathinfo($origin, PATHINFO_EXTENSION);

$dest = 'myfolder/' . $filename . '.' . $ext;

copy($origin, $dest);
Marcio Mazzucato
  • 8,841
  • 9
  • 64
  • 79
0

Use a GET request to download the image and save it to a web accessible directory on your server.

As you are using PHP, you can use curl to download files from the other server.

Peter Stuifzand
  • 5,084
  • 1
  • 23
  • 28
0

Since you've tagged your question 'php', I'll assume your running php on your server. Your best bet is if you control your own web server, then compile cURL into php. This will allow your web server to make requests to other web servers. This can be quite dangerous from a security point of view, so most basic web hosting providers won't have this option enabled.

Here's the php man page on using cURL. In the comments you can find an example which downloads and image file.

If you don't want to use libcurl, you could code something up using fsockopen. This is built into php (but may be disabled on your host), and can directly read and write to sockets. See Examples on the fsockopen man page.

brianegge
  • 29,240
  • 13
  • 74
  • 99
-5

This answer helped to me download image from server to client side.

<a download="original_file.jpg" href="file/path.jpg">
  <img src="file/path.jpg" class="img-responsive" width="600" />
</a>
Community
  • 1
  • 1
Abduhafiz
  • 3,318
  • 5
  • 38
  • 48