How get remote file content

Discussion in 'PHP' started by victor-wd, Apr 25, 2011.

  1. #1
    I want to get remote file content using socket accept, what should my simple code look like for that?
     
    victor-wd, Apr 25, 2011 IP
  2. littlejohn199

    littlejohn199 Peon

    Messages:
    42
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think this may be what you are looking for

    The following code is found at: http://php.net/manual/en/function.fsockopen.php

    <?php
    $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
    if (!$fp) {
    echo "$errstr ($errno)<br />\n";
    } else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
    echo fgets($fp, 128);
    }
    fclose($fp);
    }
    ?>
     
    littlejohn199, Apr 25, 2011 IP
  3. victor-wd

    victor-wd Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Thanks littlejohn199
    But I doubt for GET, I am going to get big files, will GET method be able to do that?
     
    victor-wd, Apr 25, 2011 IP
  4. clonepal

    clonepal Active Member

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #4
    where do you want to add content? in a mysql, csv, txt file ?

    -clonepal
     
    clonepal, Apr 25, 2011 IP
  5. victor-wd

    victor-wd Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    I want to add in same file, I mean get remote x.zip and add it in current file system as x.zip etc... same with all types of files.
     
    victor-wd, Apr 25, 2011 IP
  6. clonepal

    clonepal Active Member

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #6
    I don't understand your question. Where is current file system?
     
    clonepal, Apr 27, 2011 IP
  7. victor-wd

    victor-wd Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #7
    k sorry, here is correct question

    say we have http://host1.com/somefile.zip
    somefile.zip on server1 ( http://host1.com )

    I want to create same file on my second server http://host2.com/somefile.zip
    somefile.zip on server2 ( http://host2.com )

    Php script is located on server2 ( http://host2.com/getremotefile.php )
     
    victor-wd, Apr 27, 2011 IP
  8. TimK

    TimK Peon

    Messages:
    51
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    
    <?php
    $fileLocation="http://host1.com/somefile.zip";
    $fileName="somefile.zip";
    
    file_put_contents($fileName, file_get_contents($fileLocation));
    ?>
    
    PHP:
     
    TimK, Apr 27, 2011 IP
  9. victor-wd

    victor-wd Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #9
    thanks TimK, but as far as I know the remote file access is not allowed via fopen() or file_get_contents() on all hosting servers, this will not work on many of them, am I correct?
     
    victor-wd, Apr 27, 2011 IP
  10. TimK

    TimK Peon

    Messages:
    51
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    You are correct, in which case you could use cURL.

    
    <?php
    
    function curl_get_file_contents($URL)
        {
           $c = curl_init();
            curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($c, CURLOPT_URL, $URL);
            $contents = curl_exec($c);
            curl_close($c);
    
            if ($contents) return $contents;
                else return FALSE;
    }
    
    file_get_contents_curl($fileName);
    
    ?>
    
    PHP:
     
    TimK, Apr 27, 2011 IP
  11. victor-wd

    victor-wd Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #11
    ok, thanks a lot TimK, this one is exact for my case.
     
    victor-wd, Apr 27, 2011 IP
  12. clonepal

    clonepal Active Member

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #12
    Here is a function to see the size before downloading on server2.

    
    function remotefsize($url) {
            global $CONF, $DB, $FORM, $LNG, $TIMER;
             $urlpar = parse_url($url);
             $sch = $urlpar['scheme'];
             if (($sch != "http") && ($sch != "https") && ($sch != "ftp") && ($sch != "ftps")) return false;
             if (($sch == "http") || ($sch == "https")) {
                $headers = $this -> my_get_headers($url, 1);
                 if ((!array_key_exists("content-length", $headers))) return false;
                 return $headers["content-length"];
                 } 
            if (($sch == "ftp") || ($sch == "ftps")) {
                $server = $urlpar['host'];
                 $port = $urlpar['port'];
                 $path = $urlpar['path'];
                 $user = $urlpar['user'];
                 $pass = $urlpar['pass'];
                 if ((!$server) || (!$path))return false;
                 if (!$port)$port = 21;
                 if (!$user)$user = "anonymous";
                 if (!$pass) $pass = "phpos@";
                 switch ($sch) {
                case "ftp":
                     $ftpid = ftp_connect($server, $port);
                     break;
                 case "ftps":
                     $ftpid = ftp_ssl_connect($server, $port);
                     break;
                     } 
                if (!$ftpid) return false;
                 $login = ftp_login($ftpid, $user, $pass);
                 if (!$login) return false;
                 $ftpsize = ftp_size($ftpid, $path);
                 ftp_close($ftpid);
                 if ($ftpsize == -1) return false;
                 return $ftpsize;
                 } 
            } 
    
    
    Code (markup):
    -clonepal
     
    clonepal, Apr 27, 2011 IP
  13. victor-wd

    victor-wd Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #13
    ah, great algorithm!

    but seems it need to fix this row

    $headers = $this -> my_get_headers($url, 1);
    PHP:
     
    victor-wd, Apr 27, 2011 IP