Sarfraz
Sarfraz

Reputation: 382726

PHP: Is there a way to download files in case curl extension is not available?

Is there a way to download files in case curl extension is not available or has been disabled?

Upvotes: 0

Views: 148

Answers (2)

bisko
bisko

Reputation: 4078

This works too. Though you must have sockets enabled.

function getcontent($server,  $file,$port=80)
{
    $cont = "";
    $ip = gethostbyname($server);
    $fp = fsockopen($ip, $port);
    if (!$fp)
    {
        return "Unknown";
    }
    else
    {
        $com = "GET $file HTTP/1.1\r\nAccept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\nHost: $server:$port\r\nConnection: Keep-Alive\r\n\r\n";
        fputs($fp, $com);
        while (!feof($fp))
        {
            $cont .= fread($fp, 500);
        }
        fclose($fp);
        $cont = substr($cont, strpos($cont, "\r\n\r\n") + 4);
        return $cont;
    }
}

Usage:

getcontent('google.com', '/intl/en_ALL/images/logo.gif');

Upvotes: 2

Julien Vaslet
Julien Vaslet

Reputation: 1804

You can get the content of a remote file using:

<?php
    $file_content = file_get_contents('http://www.the-site.com/file.txt');
?>

File_get_contents on PHP.net

Upvotes: 4

Related Questions