判断文件格式并输出文件流

首先需要一个curl的方法 然后获取头信息 输出文件流

<?php
function my_request($url, $data = null,$return_header=false)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_NOSIGNAL, 1);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_TIMEOUT,10);//设置超时时间
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    if($return_header)
    {
        curl_setopt($curl, CURLOPT_HEADER, 1);//返回header信息
    }    
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}
$url="http://images2017.cnblogs.com/blog/704203/201708/704203-20170823105514714-1139981147.gif";
$stream=my_request($url,null,false);
$header=my_request($url,null,true);
$start=strpos($header,"Content-Type");
$to_header=substr($header,$start);
$end=strpos($to_header,"\r\n");
$real=trim(substr($to_header,0,$end));
if($real)
{
    header($real);
    echo($stream);
}else
{
    die("can not found file type");
}
exit();
?>
posted @ 2017-08-29 12:07  李照耀  阅读(370)  评论(0编辑  收藏  举报