views:

72

answers:

2

Hello All,

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

+4  A: 

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

Ploufka
...Which only works with fopen wrappers enabled.
Vilx-
can i use that function to download an image from a website?
Sarfraz
Yes, you can use this to download any file you like. It will fetch whatever the browser would see and return it as a string, a VERY simple function. :-)
Phil Sturgeon
+2  A: 

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');
bisko
"Accept-Encoding: gzip, deflate". If you accept gzipped content surely you would need to decode it, or will fread() do this?
Tom Haigh
I doubt you need to send Accept-Language: de-ch.
Phil Sturgeon
Had it working like thins for a while, didn't have any problems with it on any of the requests (files + pages), corrected it though.
bisko