php下载不适用于&

时间:2014-07-18 13:48:37

标签: php download

我有这个php函数从我的public_html下载文件  

function download($file){
$dir = "RepList/";
$path = $dir.$file;

if(!file_exists($path))
{
    die("Error : The file does not exists !");
    }else{
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$file");
    header("Content-Typr: application/w3g");
    header("Content-Transfer-Encoding: binary");

    readfile($path);
    }
}

if (isset($_GET['download']))
{
    if(!empty($_GET['download']))
    {
        $file = $_GET['download'];
        download($file);
    }
}

下载时我会使用以下内容:

<a class="download"  id="download'.$i.'_" href="../download.php?download='.$cc->EList[$i]->getEName().'" target="_blank">Download</a>

如果我有&#34;&amp;&#34;我的文件名中的符号,我将收到&#34;错误:该文件不存在!&#34;。 我就像1年前一样工作,现在我想修复错误。

有什么建议吗?轻松的东西快。

1 个答案:

答案 0 :(得分:3)

您应该使用urlencode()功能并将创建HTML链接更改为:

<a class="download"  id="download'.$i.'_" href="../download.php?download='.urlencode($cc->EList[$i]->getEName()).'" target="_blank">Download</a>

否则&字符在url中被视为变量分隔符