关于PHP一些数据已经发送-功能无法使用的问题-提示:Some data has already been output的解决方案

本文深入探讨了PHP中的缓冲机制,解释了如何使用ob系列函数控制脚本的数据输入输出,特别是在处理header报头发送、PDF输出等场景下避免Somedatahasalreadybeenoutput错误的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      在PHP开发中,我们经常为网站做一些功能,而且这些功能有个共同的属性,就是:在输出最终功能之前,不能有任何数据输出,如果一旦有输出,就会发生致命的错误“Some data has already been output”,比如我们常用的:验证码输出、文件下载、pdf文件输出,报头数据发送 - header('Content-Type:text/html;charset=utf-8') 等。其实这个是PHP的缓冲机制有关。

       PHP 常说的输出东西,其实会保存在php维护的内存中,称为buffer缓存缓冲。当buffer满了之后,php会自动往WebServer发送数据。

       ob系列函数则会自动控制脚本中的数据输入输出;对于一些特定的场景应用很有帮助,例如使用header的的使用,代码如下

<?php
    //写法一:
    echo "Hello";
    header("Content-type:text/html;charset=utf-8");
    //我们都知道,header报头发送之前,不能有任何输出,这样写会报错;
    //如果我们改为以下方法,则不会报错
    //写法二:
    ob_start();
    echo "Hello";
    header("Content-type:text/html;charset=utf-8");
    ob_end_flush();//输出全部内容到浏览器
?>

  第二种写法不会报错,是因为我们打开了缓冲区,echo 不会直接输出到浏览器,而是保留在服务器中,直到使用flush或ob_end_flush才会发送数据到浏览器输出。

例如:pdf 数据数据输出之前,是不能有任何输出的,有时候我们写的代码中的,常会隐藏一些输出,这就需要我们对原有的输出,进行清理,我们可用常用:ob_clean()函数(放在pdf输出之前即可,下面的错误提示,就可以使用此法种方法解决)。

错误提示:FPDF error: Some data has already been output, can't send PDF file

关于一些缓存的技术:http://www.cnblogs.com/penghcn/articles/2720202.html

[staruser@kvm10 case]$ starccm+ -batch -np 4 -mpidriver openmpi pipe_flow.sim Picked up _JAVA_OPTIONS: -Dsun.java2d.opengl=true Picked up _JAVA_OPTIONS: -Dsun.java2d.opengl=true Picked up _JAVA_OPTIONS: -Dsun.java2d.opengl=true Starting local server: /data/shared/case/opt/20.02.007-R8/STAR-CCM+20.02.007-R8/star/bin/starccm+ -np 4 -mpidriver openmpi -server -xencoded-session cGlwZV9mbG93LnNpbQ== Starting parallel server Intel MKL WARNING: Support of Intel(R) Advanced Vector Extensions (Intel(R) AVX) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library will use Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) instructions instead. Intel MKL WARNING: Support of Intel(R) Advanced Vector Extensions (Intel(R) AVX) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library will use Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) instructions instead. Intel MKL WARNING: Support of Intel(R) Advanced Vector Extensions (Intel(R) AVX) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library will use Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) instructions instead. Intel MKL WARNING: Support of Intel(R) Advanced Vector Extensions (Intel(R) AVX) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library will use Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) instructions instead. ***************************************************************************************** Warning: CPU contention on 4 processes could not be resolved. CPU affinity will be cleared on those processes. This is a strong indication that another server is already running and on some hosts there are now more processes running than there are physical CPU cores. Over-subscribing a host can severely affect performance. Even when within the limits of hardware multithreading (e.g., hyper-threading), using more processes than CPU cores will likely negatively impact performance. The recommended mode of operation is to use the same number of processes as physical CPU cores. ***************************************************************************************** MPI Distribution : Open MPI-4.1.5 Host 0 -- kvm10 -- Ranks 0-3 Process rank 0 kvm10 505984 Total number of processes : 4 Simcenter STAR-CCM+ 2502 Build 20.02.007 (linux-x86_64-2.28/clang17.0-r8) Error: Cannot open file: pipe_flow.sim (specify -new to create the file) Server working directory: '/data/shared/case' Non-recoverable error: {'Neo.Error': 'Error', 'Path': 'pipe_flow.sim', 'Processor': 0, 'Recoverability': 'Non-recoverable', 'ServerStack': ['\nlibStarNeo.so: NonRecoverableError::createInitializerProperties(Properties const&)', '\nlibStarNeo.so: NonRecoverableError::NonRecoverableError(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Properties const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)', '\nlibStarMachine.so: ServerSession::startServerHost(int, char**, ParsedArgs&)', '\nlibStarMachine.so: ServerSession::launchServer(int, char**, ParsedArgs&)', '\nlibStarMachine.so: ServerSession::main(int, char**)', '\nstar-ccm+(+0x5262)', '\nlibc.so.6(__libc_start_main+0xe5)', '\nstar-ccm+(+0x44de)'], 'ServerState': 'exiting', 'message': 'Cannot open file: pipe_flow.sim (specify -new to create the file)\nServer working directory: \'/data/shared/case\'\n', 'severity': 'ERROR'} Error: Cannot open file: pipe_flow.sim (specify -new to create the file) Server working directory: '/data/shared/case' libStarNeo.so: NonRecoverableError::createInitializerProperties(Properties const&) libStarNeo.so: NonRecoverableError::NonRecoverableError(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Properties const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) libStarMachine.so: ServerSession::startServerHost(int, char**, ParsedArgs&) libStarMachine.so: ServerSession::launchServer(int, char**, ParsedArgs&) libStarMachine.so: ServerSession::main(int, char**) star-ccm+(+0x5262) libc.so.6(__libc_start_main+0xe5) star-ccm+(+0x44de)-------------------------------------------------------------------------- MPI_ABORT was invoked on rank 0 in communicator MPI_COMM_WORLD with errorcode 36. NOTE: invoking MPI_ABORT causes Open MPI to kill all MPI processes. You may or may not see output from other processes, depending on exactly when Open MPI kills them. -------------------------------------------------------------------------- starccm+ (bash runtime process 504708) Interrupted with SIGTERM. bash runtime process 504708 calling kill -TERM for process 506004 starccm+ (bash runtime process 504707) Interrupted with SIGTERM. bash runtime process 504707 calling kill -TERM for process 505982 starccm+ (bash runtime process 504706) Interrupted with SIGTERM. bash runtime process 504706 calling kill -TERM for process 505998 error: Design Simcenter STAR-CCM+ simulation completed Server process ended unexpectedly (return code 36)
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值