2012-08-07 67 views
5

我想要下載遠程文件並將其放在我的服務器目錄中,並使用原來的文件名。我試圖使用file_get_contents($url)無法將遠程文件名獲取到file_get_contents(),然後存儲文件

問題是文件名不包含在$url中,它就像:www.domain.com?download=1726。這個URL給我,例如:myfile.exe,所以我想用file_put_contents('mydir/myfile.exe');

我如何檢索文件名?我在下載之前試過get_headers(),但我只有文件大小,修改日期和其他信息,文件名缺失。

+0

看一看捲曲它應該涵蓋你所需要的http:// php.net/manual/en/book.curl.php – Waygood 2012-08-07 09:17:39

+0

看看這個問題的cURL文件名http://stackoverflow.com/questions/1750055/with-php-curl-get-filename-from-file-header – Waygood 2012-08-07 09:21:57

回答

0

file_get_contents()如果該文件已被Web服務器預解析,則HTTP包裝器不會直接下載該文件。

看看下面的例子:如果你在刪除網頁(example.com/foobar.php)調用file_get_contents(),你會不會與源代碼foobar.php呈現,但example.com Web服務器是如何解析PHP文件。所以你只能夠檢索生成的HTML輸出。

如果文件名不存在於URL中,並且您無法從任何地方獲取它,那麼您就處於死路一條。數據不能只是從超越數據領域召喚

其他解決方案,我只能建議使用cURL庫(它是用來從你的服務器處理查詢(因爲它是一個客戶端)到其他服務器上使用URL,故名cient URL)或file sockets 。這裏是another question's answer on Stack Overflow,它描述瞭如何使用cURL獲取文件名。

此外,您可能會嘗試聯繫domain.com的管理員/維護人員/網站管理員團隊,詢問他們是否有公開可用的API來獲取文件名和其他元數據。

+0

謝謝你快速回復。我會嘗試捲曲。但我仍然想知道,瀏覽器如何知道文件名?當我把這個URL放在瀏覽器中時,我得到了一個真實文件名的提示保存窗口。因此,當我使用之前: ini_set('user_agent','Mozilla/5.0(X11; U; Linux i686; en-US; rv:1.9.0.16)Gecko/2009121601 Ubuntu/9.04(jaunty)Firefox/3.0。16' ); 我以爲我就像一個瀏覽器:) – Peter222 2012-08-07 09:40:20

+0

[RFC 2183](http://www.ietf.org/rfc/rfc2183.txt)描述了Content-Disposition:標題,它可以包含一個'filename'指令。瀏覽器解析標題的這部分,並從中獲取各種元數據。檢查我在我的答案中鏈接爲相關的答案,它描述了一種方法,如何使用PHP(和cURL)檢索此頭文件。 – Whisperity 2012-08-07 09:43:21

+0

我測試了上面的所有例子。沒有成功。我嘗試完整的這個例子類:http://stackoverflow.com/questions/6177661/how-do-download-a-file-with-curl,但不能通過cURL獲得原始文件名。即使URL包含文件名也是如此。有人可以根據任何公共文件(JPG或其他)進行工作示例。只需下載遠程並使用相同名稱在本地保存即可。我將非常感激。谷歌搜索2天,沒有想法:( – Peter222 2012-08-08 18:19:22

7

我解決了另一種方式。我發現如果url標頭中沒有內容處置,則URL存在文件名。因此,此代碼適用於任何類型的URL(不需要cURL):

$url = "http://www.example.com/download.php?id=123"; 
// $url = "http://www.example.com/myfile.exe?par1=xxx"; 
$content = get_headers($url,1); 
$content = array_change_key_case($content, CASE_LOWER); 

    // by header 
if ($content['content-disposition']) { 
    $tmp_name = explode('=', $content['content-disposition']); 
    if ($tmp_name[1]) $realfilename = trim($tmp_name[1],'";\''); 
} else 

// by URL Basename 
{ 
    $stripped_url = preg_replace('/\\?.*/', '', $url); 
    $realfilename = basename($stripped_url); 

} 

它的工作原理! :)

+0

+1。這是非常有用的,但你爲什麼不接受它作爲答案? 這個代碼有問題嗎? – aliqandil 2014-03-17 15:45:34

5

基於Peter222的代碼我寫了一個函數來獲取文件名。 可以使用$ http_response_header變量:

function get_real_filename($headers,$url) 
{ 
    foreach($headers as $header) 
    { 
     if (strpos(strtolower($header),'content-disposition') !== false) 
     { 
      $tmp_name = explode('=', $header); 
      if ($tmp_name[1]) return trim($tmp_name[1],'";\''); 
     } 
    } 

    $stripped_url = preg_replace('/\\?.*/', '', $url); 
    return basename($stripped_url); 
} 

用法:($ http_response_header會的file_get_contents填寫())

$url = 'http://example.com/test.zip'; 
$myfile = file_get_contents($url); 
$filename = get_real_filename($http_response_header,$url) 
相關問題