[php] 取得網址內的檔案名稱 - 網址內的檔案名稱跟相關資訊 (fileName)


如果現有一網址:

https://www.google.com.tw/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png?from=blog.webgolds.com

該如何拆解,不要有後面的參數,而可以得到headers、完整檔名、檔案名稱、副檔名?
例如變成:
完整檔名:googlelogo_color_272x92dp.png
檔案名稱:googlelogo_color_272x92dp
副檔名:png


<?php


//取得網址內的檔案名稱跟相關資訊
function getRemoteFileInfo($sourceUrl){

    $infoArr = array();
    $infoArr['file_headers'] = @get_headers($sourceUrl);
    $parts = parse_url($sourceUrl);

    $str = $parts['path'];
    $infoArr['file_name'] = basename($str); // to get file name
    $infoArr['ext'] = pathinfo($str, PATHINFO_EXTENSION); // to get extension
    $infoArr['name2'] =pathinfo($str, PATHINFO_FILENAME); //file name without extension
    
    return $infoArr;
}


使用方式:

echo '<pre>';
print_r(getRemoteFileInfo('https://www.google.com.tw/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png'));


輸出結果:
Array
(
    [file_headers] => Array
        (
            [0] => HTTP/1.0 200 OK
            [1] => Accept-Ranges: bytes
            [2] => Content-Type: image/png
            [3] => Content-Length: 13504
            [4] => Date: Sat, 29 Jul 2017 07:29:18 GMT
            [5] => Expires: Sat, 29 Jul 2017 07:29:18 GMT
            [6] => Cache-Control: private, max-age=31536000
            [7] => Last-Modified: Thu, 08 Dec 2016 01:00:57 GMT
            [8] => X-Content-Type-Options: nosniff
            [9] => Server: sffe
            [10] => X-XSS-Protection: 1; mode=block
            [11] => Alt-Svc: quic=":443"; ma=2592000; v="39,38,37,36,35"
        )

    [file_name] => googlelogo_color_272x92dp.png
    [ext] => png
    [name2] => googlelogo_color_272x92dp
)




cURL
get only filename from url  without any variable values(hash)

回上一頁

相關文章:

喜歡這篇文章嗎?立即分享

Posted : / Views: 823
Last updated :2017-07-29