2012-09-13 44 views
0

我想知道,我如何訪問我的服務器的根目錄以外的文件cURL - 打開服務器根目錄以外的文件

例如,/home/username/FILE.mp3

我目前使用的打開文件的代碼如下:

<?php 
$location = "ABSOLUTE-PATH/FILE.mp3"; 

$ch = curl_init($location); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_FILETIME, true); 
$data = curl_exec($ch); 
$timestamp = curl_getinfo($ch, CURLINFO_FILETIME); 
curl_close($ch); 
if ($data === false) { 
    echo 'CURL Failed'; 
    exit; 
} 
//Get file size 
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) { 
    $contentLength = (int)$matches[1]; 
} 

$begin = 0; 
$end = $contentLength - 1; 

if (preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) 
{ 
    $begin = intval($matches[1]); 
    if (!empty($matches[2])) 
    { 
     $end = intval($matches[2]); 
    } 
} 

if (isset($_SERVER['HTTP_RANGE'])) 
{ header('HTTP/1.1 206 Partial Content'); } 
else 
{ header('HTTP/1.1 200 OK'); } 

header('Accept-Ranges: bytes'); 
header('Content-Length: ' . $contentLength); 
header("Content-Range: bytes $begin-$end/$contentLength"); 
header('Content-Type: audio/mpeg'); 
header('Cache-Control: public, must-revalidate, max-age=0'); 

if ($timestamp != -1) { //otherwise unknown 
    header("Last-Modified: " . gmdate("D, d M Y H:i:s", $timestamp) . " GMT"); 
} 

ob_clean(); 
flush(); 
echo ($data); 
exit; 
?> 

我知道作爲一個事實,即捲曲不使用相對路徑,我假設根目錄之外的文件被視爲相對鏈接。

回答

0

你爲什麼要用cURL?通常,cURL將用於通過HTTP與不同的服務器進行通信。爲什麼不使用file,file_get_contents,fopen或類似的?

相關問題