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

    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="http://www.domain.com:3000">

    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:
    <?php

    $url 
    "http://www.domain.com:3000/pic.jpg";
    $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 (!
    $id){
        
    $File fopen("capture.jpg","wb");
    }

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


        while (!
    feof($fd) )
    {
      
    $contents fread($fd4096);
      
    fwrite$File$contents );
    }
       
    fclose$fd );
      
    fclose$File );
       echo 
    "..Done";  

    ?>
    Code:
    Warning:  fopen(): Circular redirect, aborting. in /usr/home/login/public_html/pic.php on line 11
    
    Warning:  fopen(http://domain.com:3000/pic.jpg): 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
    Location
    Kuwait
    Posts
    5,099
    I don't know exactly what's causing the error. I have a suspicion that its the server that is running on domain.com:3000.

    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
    Posts
    193
    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:
    <html>
    <head>
    <title>Image</title>
    </head>

    <body>
    <?php

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

    $fd fsockopen($host,$port,$errnum,$errstr,$timeout);
    if(!
    is_resource($fd)) {
        exit(
    "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: http://www.somesite.com".$br ;
        
    $headers .= "X-Something: Hello from John".$br.$br;

        
    fputs($fd,$headers);

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

    echo (
    $contents);

    ?>
    </body>
    </html>
    Also, I can pull the image up without any problems if I call it like so:

    Code:
    <img src="http://site.com:3000/pic.jpg">
    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
  •