我正在使用以下代码从服务器下载 mp3 文件。该代码在我的本地系统(Windows 操作系统)中运行良好。
但是,当我将代码移动到服务器(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)