开发者

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

Is there a way to download f开发者_如何学JAVAiles in case curl extension is not available or has been disabled?


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


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');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜