0

我在restler中得到了以下功能

/**
 * get updateFiles by name
 *
 * one could get the last update file
 *
 * @status 201
 * @return file
 */
function getupdateFile($filename) {

    $file = 'Plakat.jpg';

    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');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
    else {
        return "<h1>Content error</h1><p>The file does not exist!(".dirname(__FILE__).'/'.($file).")</p>";
    }
}

问题是文件存在权限是正确的,但没有强制下载文件。失败在哪里?它总是返回“文件不存在,但确实存在。我搜索了强制下载 php 的不同问题,但这似乎是 restler 的问题?

谢谢英戈

4

1 回答 1

0

您可以尝试下面的代码,它恰好可以正常工作

$file = 'Plakat.jpg';
if(file_exists($file))
{
    header("Content-Type: image/png");
    header('Content-Disposition: attachment; filename="ImageName.png"');
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    readfile($file);
}

exit();
于 2013-05-04T12:57:04.190 回答