今天,我想创建一些PHP脚本,它将直接强制下载mp3文件。无法通过php脚本访问文件
文件我想下载放入声音文件夹中,我可以通过http://playall.pl/sounds/ThemadpixprojectBadChick.mp3
访问,但是当它涉及到PHP脚本 - 函数file_exists说,这个文件是不是此服务器上。
我该如何解决?使用它来下载文件
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file_name));
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;
而获得的文件名和文件的URL
$file_name = ''.$sound['Sounds']['artist'].' - '.$sound['Sounds']['title'].'.mp3';
$file_name = preg_replace('/\s+/', '_', $file_name);
$file = 'http://playall.pl'.$this->webroot.'sounds/'.$sound['Sounds']['src_url'];
为什么你不重定向浏览器? – 2014-09-25 23:25:51
因为我想强制按钮单击下载此文件。 – GamaPL 2014-09-25 23:28:38
并添加,即时通讯使用CakePHP框架。 – GamaPL 2014-09-25 23:29:09