dongzhangji4824 2013-11-06 06:15
浏览 36

返回文件顶部插入的字符(换行符)会破坏文件PHP吗?

I have a function that returns an image from a database, the image is 100% fine in the database however when it is sent to the browser a newline (return) character is inserted in the very beginning and it corrupts the file.

The code to return the file is:

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

Commented the above out as when it is uncommented it doesn't even let the browser download the file.

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;
            }

Related Questions: Corrupted File from PHP Header File returned from database? https://stackoverflow.com/questions/19768650/zend-caching-of-images-gives-problems-once-the-site-goes-down-for-a-while

  • 写回答

1条回答 默认 最新

  • drnf09037160 2013-11-06 07:20
    关注

    I would do the following. You have an extra space somewhere in your PHP file.
    It can be a bit tricky to find, so start putting some

    echo "here" 
    

    in your code to see if this newline is found before or after the echo. Keep moving he echo statement up or down the file until you can see where it is (it might be in an include file, so you will have to track it down.

    ---------------START OF FILE

    评论

报告相同问题?

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题