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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀