Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2001

    How to fetch an image in PHP using fsockopen

    Can somebody tell me how to go about copying a file to a server using PHP? Here's an example of what works when calling it through an image tag:

    <img src="">

    I need to be able to copy it from the external server (note the odd port) to the local one. I've tried using the following script but it keeps coming up with the errors listed below which may be something to do with the port.

    PHP Code:

    $id "";  //name of the file u want to be placed on your server
    $fd fopen$url"rb" )
        or die(
    "The remote file does not exist. Please check your URL and try again.");

    if (!
    $File fopen("capture.jpg","wb");

    $File fopen("capture_$id.jpg","wb");

        while (!
    feof($fd) )
    $contents fread($fd4096);
    fwrite$File$contents );
    fclose$fd );
    fclose$File );

    Warning:  fopen(): Circular redirect, aborting. in /usr/home/login/public_html/pic.php on line 11
    Warning:  fopen( failed to open stream: Undefined error: 0 in /usr/home/login/public_html/pic.php on line 11
    The remote file does not exist. Please check your URL and try again.
    Line 11 is this: $fd = fopen( $url, "rb" )

    Any help would be greatly appreciated.

  2. #2
    Join Date
    Jul 2003
    I don't know exactly what's causing the error. I have a suspicion that its the server that is running on

    A quick look at the PHP documentation doesn't specifically state that fopen() will accept port numbers in the URL, so you might need to use fsockopen().

    Another option would be to check if you were redirected or not. There is an example at this manual page that describes the checking of redirection.

    If all else fails (iaef) -- you can always go with snoopy.
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  3. #3
    Join Date
    Oct 2001
    I made some changes to my code which I thought would fix the problem, but I'm still having trouble. When I load this script, it creates the image file and it's the correct size, but it's in ASC-II format and not binary format. Do you have any ideas what the problem is?

    PHP Code:


    $target "pic.jpg";
    $port 3000;
    $timeout 60;
    $protocol "HTTP/1.0";

    $fd fsockopen($host,$port,$errnum,$errstr,$timeout);
    is_resource($fd)) {
    "Connection failed: ".$errnum." ".$errstr) ;
    } else {

    $br "\r\n";
    $headers "GET ".$target." ".$protocol.$br ;
    $headers .= "Accept: image/jpeg".$br ;
    $headers .= "Accept-Language: en-us".$br ;
    $headers .= "Host: ".$host.$br ;
    $headers .= "Connection: Keep-Alive".$br ;
    $headers .= "User-Agent: Socket-PHP-browser 1.0".$br;
    $headers .= "Referer:".$br ;
    $headers .= "X-Something: Hello from John".$br.$br;


    $contents "";
        while (!
    feof($fd)) {
    $contents .= fgets($fd2048);

    echo (

    Also, I can pull the image up without any problems if I call it like so:

    <img src="">
    it's just that I have to copy it to the server and for some reason it's not converting it into binary format.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts