PHP:如果没有curl扩展名,有没有办法下载文件?

时间:2009-12-17 08:34:52

标签: php

如果卷曲扩展程序不可用或已被禁用,有没有办法下载文件?

2 个答案:

答案 0 :(得分:4)

您可以使用以下方式获取远程文件的内容:

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

File_get_contents on PHP.net

答案 1 :(得分:2)

这也有效。虽然你必须启用套接字。

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;
    }
}

用法:

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