PHP中强大的下载功能?

PHP中强大的下载功能?,php,download,Php,Download,我试图从PHP中以编程方式下载一个图像文件,然后在本地处理它 已编辑:以前的功能已替换为下面建议的功能 我有这个功能: function downloadFile ($url, $path) { $result = false; $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'; $ch = curl_init($url); curl_seto

我试图从PHP中以编程方式下载一个图像文件,然后在本地处理它

已编辑:以前的功能已替换为下面建议的功能

我有这个功能:

function downloadFile ($url, $path) {
    $result = false;
    $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_exec( $ch ) ;
    if(!curl_errno($ch))  
    {
        $type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); 
        if ( stripos($type, 'image') !== FALSE )
        {
            // probably image

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_NOBODY, false);  
            curl_setopt($ch, CURLOPT_HEADER, false);    
            $fp=fopen($path,'wb');
            curl_setopt($ch, CURLOPT_FILE, $fp); 
            curl_exec($ch);
            fclose($fp);
            if ( exif_imagetype($path) != FALSE )
            {
                // 100% image
                $result = true;
            }
            else
            {
                // not an image
                unlink($path);
            }
        }
    }

    curl_close($ch);
    return $result;
}
我真正需要的是一个强大的函数,可以处理任何类型的图像,如果url无效,没有图像

更新:

我用下面建议的函数更改了我的downloadFile函数。在我的本地计算机上它工作得很好,但在我的服务器上它失败了:/I我正在下载一些0字节的文件

更新2:

仍然没有进展,在服务器上由于某种原因没有下载文件。除了拥有curl,在服务器上运行它还有其他要求吗?
我还收到一个“2006-MySQL服务器消失了”,我认为这是由于下载问题造成的。

我认为您需要安装openssl并使用PHP进行配置。

使用

此函数还检查图像的url。返回真/假(图像或非图像)

请求:

if ( !downloadFile($url, $path) )
{
     // an error
} 

最后,这是最有效的下载功能:

/*
 * This function downloads the image to the plugin/topposts/temp folder
 */
function downloadFile ($url, $path) {
 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $con = curl_exec($ch);
 curl_close($ch);
 file_put_contents($path, $con);
 return true;
}

不检查类型,但有效。谢谢你的帮助。

你为什么要用
http
替换
https
?我试图这样做是为了解决https URL不起作用的问题,但它没有解决问题。直接获取
https
时会出现什么错误?这样替换是一个可怕的主意-例如,如果有人想要获取,
http://www.example.com/https-indicator.png
?您试过了吗?谢谢Andrey,但由于某些原因,我无法在我的wamp上激活curl扩展,而且如果不在本地进行测试,我无法投入生产:(什么原因?检查libeay32.dll和ssleay32.dll是否存在。同时启用php_openssl模块。是的,它们都存在。在apache\bin和php5.3.13文件夹中。尝试从命令行运行空php.exe。它是否显示任何消息?php日志显示什么?检查php.ini中的扩展名_dir
/*
 * This function downloads the image to the plugin/topposts/temp folder
 */
function downloadFile ($url, $path) {
 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $con = curl_exec($ch);
 curl_close($ch);
 file_put_contents($path, $con);
 return true;
}