返回插入文件顶部的字符(换行符),这会损坏文件 PHP



我有一个从数据库返回图像的函数,图像在数据库中是 100% 好的,但是当它发送到浏览器时,在开头插入了一个换行符(返回)字符,它会损坏文件。

返回文件的代码为:

//if(ob_get_length() > 0)
            //{
                //ob_clean();
            //}
注释

掉了上述内容,因为当它未注释时,它甚至不允许浏览器下载文件。

function header_file($data, $file_data)
            {
                $last_modified = gmdate('D, d M Y H:i:s', $file_data['unix_last_modified_time'])." GMT";
                // update the accessed record
                $GLOBALS['db']->query("update file_uploads set file_upload_accessed_count = file_upload_accessed_count + 1 where file_upload_id = '".$GLOBALS['id']."'");
                // if browser question if it's up to date
                if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
                {
                    // parse header
                    $if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
                    if ($if_modified_since == $last_modified)
                    {
                        // the browser's cache is still up to date
                        header("HTTP/1.0 304 Not Modified");
                        header("Cache-Control: max-age=86400, must-revalidate");
                        exit;
                    }
                }
                header("Cache-Control: max-age=86400, must-revalidate");
                header("Last-Modified: ".$last_modified);
                header("Content-Type: ".$file_data['file_upload_type']);
                if($file_data['file_upload_type'] == 'application/x-shockwave-flash')
                    header("Content-Disposition: inline; filename="".str_replace(' ','_',$file_data['file_upload_name']).""");
                else
                    header("Content-Disposition: attachment; filename="".str_replace(' ','_',$file_data['file_upload_name']).""");
                // send data to output
                echo $data;
                exit;
            }

相关问题:从数据库返回的 PHP 头文件中的文件损坏?https://stackoverflow.com/questions/19768650/zend-caching-of-images-gives-problems-once-the-site-goes-down-for-a-while

我会执行以下操作。 您在PHP文件中的某个地方有一个额外的空间。
找到它可能有点棘手,所以开始放一些

echo "here" 

以查看是在 echo 之前还是之后找到此换行符。继续向上或向下移动 echo 语句,直到您可以看到它的位置(它可能在包含文件中,因此您必须向下跟踪它。

---------------文件开头

最新更新