0

该脚本不断返回“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';

    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.');
    }
?>

我不明白路径有什么问题。

4

2 回答 2

0

fileexist() 函数获取主机上文件的路径,而不是完整的 URL 试试这个

$filePath = 'audio/far_sighted_michael_pitluk.mp3';
于 2013-07-08T09:23:43.993 回答
0

如果您从您$filePath开始,http://您可能需要启用allow_url_fopen. PHP 手册.

尝试

ini_set("allow-url-fopen", true);
// Fetch the file info.
$filePath = 'http://michaelpitluk.com/audio/far_sighted_michael_pitluk.mp3';

编辑:试试这个:

<?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.');
    }
?>
于 2013-07-08T09:34:05.583 回答