首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cURL -打开服务器根目录之外的文件

cURL -打开服务器根目录之外的文件
EN

Stack Overflow用户
提问于 2012-09-13 18:11:45
回答 1查看 541关注 0票数 0

我想知道,如何访问服务器根目录之外的文件

例如,/home/username/FILE.mp3

我当前用于打开文件的代码如下:

代码语言:javascript
复制
<?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;
?>

我知道cURL不使用相对路径,我假设根目录外的文件被视为相对链接。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-13 18:15:01

为什么要使用cURL?通常,cURL将用于通过HTTP与另一台服务器通信。为什么不使用filefile_get_contentsfopen或类似的?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12412289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档