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

报告相同问题?