English 中文(简体)
PHP 文件下载在 Linux 不工作
原标题:PHP file download not working in Linux
  • 时间:2012-05-23 10:27:18
  •  标签:
  • php

我使用下面的代码从服务器下载 mp3 文件。 该代码在我的本地系统( Windows OS) 运行良好 。

然而, 当我将代码移动到服务器( Linux) 时, 我正在获取一个找不到错误的文件。 我确信文件路径正确, 文件可以读取

if ($fd = fopen ($filePath, "r")) {
    $fsize = filesize($filePath);
    $path_parts = pathinfo($filePath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {                 
        case "mp3":                 
        header("Content-type: audio/mpeg"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename="".$originalFileName."""); // use  attachment  to force a download
        break;
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename="".$originalFileName.""");
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd) 
问题回答

下面我要写出这个代码:

// A couple of sanity checks on the file path, return a meaningful error message for debugging
if (!file_exists($filePath)) {
  header( HTTP/1.1 404 Not Found );
  exit( The requested file was not found );
} else if (!is_readable($filePath)) {
  header( HTTP/1.1 403 Forbidden );
  exit( The requested file is not accessible );
}

// Get the content type from the extension
// Consider using finfo instead: http://php.net/manual/en/ref.fileinfo.php
$contentTypes = array(
   mp3  =>  audio/mpeg ,
   jpg  =>  image/jpeg ,
   gif  =>  image/gif 
  // etc etc
);
$ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
$contentType = (isset($contentTypes[$ext])) ? $contentTypes[$ext] :  application/octet-stream ;

// Content-* headers
header("Content-Length: ".filesize($filePath));
header("Content-Type: {$contentType}");
// You will always want the same Content-Disposition: header, regardless of file type
// The only time you would need a different one is if you want to serve "inline" content
header("Content-Disposition: attachment; filename="{$originalFileName}"");

// No-Cache headers
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

// We don t want the script to time out on large files
set_time_limit(0);

// Output the file
readfile($filePath);
exit;




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签