php – 标题下载文件已损坏

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 标题下载文件已损坏脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正试图通过我的数据库中的标题下载文件.当我将下载代码改为使用OOP的下载代码时,我不确定为什么我下载的文件全部损坏但是当我的代码是非OOP时这样做很好.

这是我获取文件ID并调用下载函数的地方:(handleDownload.PHP)

if (isset($_GET['id'])) {
        $id = $_GET['id'];
        //pump id into function getDBFiles to pull file wITh matching id
        $fileData = $Download->getDBFiles($id);
        header('Content-tyPE:"' . $fileData[2]. '"');
        header('Content-Disposition: attachment; filename="' . $fileData[1]. '"'); 
        echo $fileData[0];
        exit;
      }

这是从数据库提取文件函数(download.PHP)

public function getDBFiles($id) {
            global $database;
            $sql = "SELECT * From ".self::$table_name." WHERE resume_id ='" . $id . "'";
            $result = $database->query($sql);
            if ($row = $result->fetch_array(MysqLI_ASSOC)) {  
                $name = $row['resume_title'];
                $type = $row['file_type'];
                $content = $row['resume_data']; //content of file
                //$size = $row['file_size']; //file size
                return array($content,$name,$type);
            }
        }
 $Download = new Download();
 $download =& $Download;

如果代码全部在一个页面中,如下所示,代码工作正常

if (isset($_GET['id'])) {
    $id = $_GET['id'];
    MysqLi_select_db($con,"apples");

    $query = "SELECT * From resume where resume_id ='" . $id . "'";
    $result = MysqLi_query($con,$query) or die('Error,query Failed');


    if ($row = $result->fetch_array(MysqLI_ASSOC)) {
        $name = $row['resume_title'];
        $type = $row['file_type'];
        $content = $row['resume_data']; //content of file
        $size = $row['file_size']; //file size
        header('Content-Type:"' . $type . '"');
        //header('Content-length:"' . $size . '"');
        header('Content-Disposition: attachment; filename="' . $name . '"');
        //var_dump($row);
        echo $content;
    }
}

更新:
我现在得到的下载文件已损坏,而不是空白文件.这是不同下载代码输出相同文件的方式.顶部的一个来自OOP代码,而另一个来自工作的非OOP版本.

这是我的完整下载代码.

try {
    //execute retrieval of files from database
    $Download-> showDBFiles();
    //pass results to output array 
    $output = $Download->getMessages();
    //if id is set then get file from database
    if (isset($_GET['id'])) {
        $id = $_GET['id'];
        //pump id into function getDBFiles to pull file with matching id
        $fileData = $Download->getDBFiles($id);
        header('Content-Type:"' . $fileData[2]. '"');
        header('Content-Disposition: attachment; filename="' . $fileData[1]. '"'); 
        echo $fileData[0];
        die();
      }
} catch (Exception $e) {
    $result[] = $e->getMessages();
}

调用函数后,我会用foreach循环回显输出(下载链接)

<h2>Output</h2>
<?PHP if ($output) { ?>
<ul class="result">
    <?PHP
    foreach ($output as $message) {
        $id = $message['id'];
        $name = $message['name'];
        ?>
    <li><a href="handleDownload.PHP?id=<?PHP echo $id; ?>"><?PHP echo $name; ?></a></li>

    <?PHP }
?>
</ul>
在您的非OOP解决方案中检查PHP文件中的前导空格.

由于前导空格,以下代码生成损坏的文件.

<?PHP
if (isset($_GET['id'])) {...

这也适用于关闭PHP标记之后的空格(你不应该使用它).

这些字符将由您的浏览器提交,包含在下载流中并破坏整个文件.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 标题下载文件已损坏全部内容,希望文章能够帮你解决php – 标题下载文件已损坏所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。