如何强制下载PHP?
此脚本不断返回“else”响应。为什么我的病情持续恶化:如何强制下载PHP?,php,download,Php,Download,此脚本不断返回“else”响应。为什么我的病情持续恶化: <?php $fileName = $_GET['far_sighted_michael_pitluk.mp3']; $fileDisplayName = "Far-Sighted"; // Fetch the file info. $filePath = 'http://michaelpitluk.com/audio/far_sighted_michael_pitluk.mp3'; i
<?php
$fileName = $_GET['far_sighted_michael_pitluk.mp3'];
$fileDisplayName = "Far-Sighted";
// Fetch the file info.
$filePath = 'http://michaelpitluk.com/audio/far_sighted_michael_pitluk.mp3';
if(file_exists($filePath)) {
$fileName = basename($filePath);
$fileSize = filesize($filePath);
// Output headers.
header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileDisplayName);
// Output file.
readfile ($filePath);
exit();
}
else {
die('The provided file path is not valid.');
}
?>
我不明白路径有什么问题。fileexist()函数获取主机上文件的路径,而不是完整的URL
试试这个
$filePath = 'audio/far_sighted_michael_pitluk.mp3';
如果您的
$filePath
以http://
开头,您可能需要启用allow\u url\u fopen
试一试
编辑:
试试这个:
<?php
$fileName = $_GET['far_sighted_michael_pitluk.mp3'];
$fileDisplayName = "Far-Sighted.mp3";
// Fetch the file info.
$filePath = '../audio/far_sighted_michael_pitluk.mp3';
if(file_exists($filePath)) {
$fileName = basename($filePath);
$fileSize = filesize($filePath);
// Output headers.
header("Cache-Control: private");
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileDisplayName);
// Output file.
readfile ($filePath);
exit();
}
else {
die('The provided file path is not valid.');
}
?>
你的服务器上有mp3吗?$\u GET['four\u-sized\u-michael\u-pitluk.mp3'代码>您确定param是这样命名的吗?真奇怪。@Dagon是的。然后使用本地文件path@Dagon我使用了“.audio/far\u sided\u michael\u pitluk.mp3”,这在浏览器开始下载某些东西时起作用,但最终并不是mp3。我的同事不知道怎么读。你知道为什么吗?在我使用“.audio/远视迈克尔皮特鲁克.mp3”时工作过谢谢!你能想出浏览器开始下载但文件不是.mp3的原因吗?我的mac电脑只是告诉我这是一个文档,当我打开它时,它说找不到该应用程序。@MichaelPitluk$fileDisplayName=“远视”
应该是$fileDisplayName=“远视.mp3”
@MichaelPitluk AFAIK Browser如果不知道如何处理即将到来的数据或内容类型设置为允许下载,则开始下载不幸的是,该条件仍然失败。虽然,我不明白为什么。看起来应该可以用了,有什么想法吗?@MichaelPitluk只有当你的服务器可以通过URL
访问该文件时,这才有效。从上一篇文章来看,该文件似乎位于您的服务器上,您可能不需要它。仅此而已!令人惊叹的!你能编辑你的答案,把它包括进去吗?所以我可以选择它作为答案?我只需要使用本地路径并更改fileDisplayName@MichaelPitluk完整代码。我还更改了内容类型
以适用于所有浏览器,$fileDisplayName
非常适用,谢谢!这是我运行过的第一个真正的php脚本,非常感谢:)
<?php
$fileName = $_GET['far_sighted_michael_pitluk.mp3'];
$fileDisplayName = "Far-Sighted.mp3";
// Fetch the file info.
$filePath = '../audio/far_sighted_michael_pitluk.mp3';
if(file_exists($filePath)) {
$fileName = basename($filePath);
$fileSize = filesize($filePath);
// Output headers.
header("Cache-Control: private");
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileDisplayName);
// Output file.
readfile ($filePath);
exit();
}
else {
die('The provided file path is not valid.');
}
?>