开发者

how to obtain the download name from an url php coding

开发者 https://www.devze.com 2023-02-26 04:41 出处:网络
ive been trying to get the name of files from a url, i found it simple with base name until i came urls that has no sign of the true name, intil downloaded

ive been trying to get the name of files from a url, i found it simple with base name until i came urls that has no sign of the true name, intil downloaded

here is an example of the links i found

here the true name is youtubedownloadersetup272.exe http://qdrive.net/index.php/page-file_share-choice-download_file-id_file-223658-ce-0

as you can see it shows no name until download.

ive been searching a lot, i got desperated of finding nothing, ill apreciate if someone can point me the way thanks.

i sorry to bother again but i foun this link from download.com and i dont se the filename using curl

<?php

function getFilename($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    $data = curl_exec($ch);

 echo $data;    
preg_match("#filename=([^\n]+)#is", $data, $matches);

    return $matches[1];
}

echo getFilename("http://software-files-l.cnet.com/s/software/11/88/39/66/YouTubeDownloaderSetup272.exe?e=1302969716&h=89b64b6e8e7485eab1e560bbdf68281d&lop=link&ptype=1901&ontid=2071&siteId=4&edId=3&spi=fdc220b131cda22d9d3f715684d064ca&pid=11883966&psid=10647340&fileName=YouTubeDownloaderSetup272.exe"); 

?>

it returns this with echo $data

HTTP/1.1 200 OK Server: Apache Accept-Ranges: bytes Content-Disposition: attachment Content-Type: application/download Age: 866 Date: Sat, 16 Apr 2011 10:16:54 GMT Last-M开发者_如何学JAVAodified: Fri, 08 Apr 2011 18:04:41 GMT Content-Length: 4700823 Connection: keep-alive

if i understood the scrip you gave me it wont work because it has no filename, is there a way to get the name with out having to do regex or parsing the url (YouTubeDownloaderSetup272.exe?e.........), like the scrip you gave me ?


You need to use curl, or some other library to request the file and look at the headers of the response.

You'll be looking for a header like:

Content-Disposition: attachment; filename=???

Where the question marks are the name of the file.

(You will still have to download the file, or at least look like you are downloading the file.)


function getFilename($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    $data = curl_exec($ch);

preg_match("#filename=([^\n]+)#is", $data, $matches);

    return $matches[1];
}

echo getFilename("http://qdrive.net/index.php/page-file_share-choice-download_file-id_file-223658-ce-0"); //YouTubeDownloaderSetup272.exe

For download.com...

function download_com($url){

    $filename = explode("?", $url);
    $filename = explode("/", $filename[0]);
    $filename = end($filename);

    return $filename;

}

echo download_com("http://software-files-l.cnet.com/s/software/11/88/39/66/YouTubeDownloaderSetup272.exe?e=1302969716&h=89b64b6e8e7485eab1e560bbdf68281d&lop=link&ptype=1901&ontid=2071&siteId=4&edId=3&spi=fdc220b131cda22d9d3f715684d064ca&pid=11883966&psid=10647340&fileName=YouTubeDownloaderSetup272.exe");
0

精彩评论

暂无评论...
验证码 换一张
取 消