可从php头下载的MP3文件不起作用 [英] Downloadable Mp3 Files from php headers not working

查看:127
本文介绍了可从php头下载的MP3文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hello there
好​​的,所以这是交易
我的服务器上有我的mp3文件,每个文件都在它自己的文件夹中。
在该文件夹中是mp3和带有以下脚本的php文件:

hello there okay so this is the deal i have my mp3 files on my server and each one is in its own folder. in that folder is the mp3 and a php file with the following script:

<?php
// We'll be outputting a PDF
header('Content-type: audio/mp3');

// It will be called file.mp3
header('Content-Disposition: attachment; filename="mysong.mp3"');
// The PDF source is in original.mp3
readfile("mysong.mp3");
?>

问题是,当我点击去那个php页面时,它会自动下载mp3文件,当它下载它时会下载一个300KB的文件,但是当我进入mp3文件的实际链接时,它会在浏览器中完美播放,所以即时猜测PHP文件提供的头文件是错误的。 p>

the problem is that when i click to go that php page the headers are suppose to make it so that it automatically downloads the mp3 file yet when it downloads it downloads a 300KB file but when i go to the actual link for the mp3 file it plays it perfectly in the browser so im guessing something is wrong with the php file giving the headers.

推荐答案

尝试从 http://us.php.net/readfile

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

这篇关于可从php头下载的MP3文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆