dtf24224 2017-06-03 16:49
浏览 71
已采纳

使用php从url检索文件信息

i am trying to retrieve information of file from the url containing the file. but how can i get the information of file before downloading it to my server. i need file information like file size,file type etc

i had found the code to validate and download file but how to get information from it before downloading file actually to server

<?php
function is_url_exist($url)
    {
    $ch = curl_init($url);    
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if($code == 200)
        {
        $status = "true";
        }
    curl_close($ch);
    if ( $status == true)
        {
        $name = "abc.png";
        if (file_put_contents("uploads/$name", file_get_contents($url)))
            echo "file uploaded";
        else
            echo "error check upload link";
        }
    }
$url = "http://theonlytutorials.com/wp-content/uploads/2015/06/blog-logo1.png";
echo is_url_exist($url);
?>
  • 写回答

2条回答 默认 最新

  • dragon7713 2018-12-09 10:15
    关注

    for this you can fech site headers that will tell all the details of the file i.e size file type etc....

    you can do this in php by fetching headers by

    format to fech headers is get_headers($url) or get_headers($url) and as the code shown in the previous answer is correct

    <?php
       $url = "http://theonlytutorials.com/wp-content/uploads/2015/06/blog-logo1.png";
    $headers = get_headers($url,1);
    print_r($headers);
    ?>
    

    this will give you data in array/json format that will store all the required details you need..

    you can refer to http://php.net/manual/en/function.get-headers.php for details

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题