Skip to main content

Copy Image from Remote Server Over HTTP



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.



Source: Tips4all

Comments

  1. 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.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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.

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

    ReplyDelete
  5. 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.

    ReplyDelete
  6. Assuming you're using .NET, which you may not be:

    WebClient wc = new WebClient();
    byte[] data = wc.DownloadData(path);
    MemoryStream ms = new MemoryStream(data);
    return (Bitmap)Bitmap.FromStream(ms);

    ReplyDelete
  7. 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.

    ReplyDelete
  8. 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.

    ReplyDelete
  9. 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

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex