用于下载和删除文件的PHP脚本在桌面上工作,但在android上失败

用于下载和删除文件的PHP脚本在桌面上工作,但在android上失败,php,android,Php,Android,我有一个脚本,下载一个pdf文件,然后删除它。该脚本创建一个类文档的实例,然后调用Document的download()方法。 代码如下: public function download($data = null){ if (file_exists($_SESSION['__config__']'html_root']."/".$this->getFilePath().$this->file)){ $this->downloadFile();

我有一个脚本,下载一个pdf文件,然后删除它。该脚本创建一个类文档的实例,然后调用Document的download()方法。 代码如下:

public function download($data = null){
    if (file_exists($_SESSION['__config__']'html_root']."/".$this->getFilePath().$this->file)){
        $this->downloadFile();
        $this->deleteFile();
    }
    else {
        $_SESSION['no_file']['file'] =  $_SESSION['__config__']['html_root']."/".$this->getFilePath().$this->file;
        header("Location: no_file.php");
    }
}

public function downloadFile(){
    $mime = MimeType::getMimeContentType($this->file);
    $fp = "../../".$this->getFilePath().$this->file;
    header("Content-Type: ".$mime['ctype']);
    header("Content-Disposition: attachment; filename=".$this->file);
    header("Content-Transfer-Encoding: Binary");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Pragma: public");
    readfile($fp);
    //exit;
}

public function deleteFile(){
    if (file_exists("../../".$this->getFilePath().$this->file)){
        unlink("../../".$this->getFilePath().$this->file);
    }
}
当我在Android上下载该文件时,会得到一个损坏的PDF文件,其中包含no_file.php html代码。
如果没有
$this->deleteFile()
我也可以在Android上获得正确的pdf文件。

我的猜测是:由于您没有等待下载完成,所以在传输所有信息之前删除了文件。是readfile阻塞吗?我想是的,但是。。。为什么只在Android上运行?您现在可以试着注释deleteFile()函数,然后测试它是否在Android上运行吗?我的评论可能是胡说八道,因为缓冲区应该是非螺旋式填充的。已经完成:如果我对该函数进行注释,它也可以在Android上工作。如果我在deleteFile()调用之前加上一行
sleep(5)
,那么在Android上下载要比平常多花5秒钟。但只在安卓岛上。这真是一种奇怪的行为。我不知道Android浏览器与桌面版本有什么不同,因此我无法给出更多建议。