简体   繁体   English

在任何地方放置标题('Content-Type:image / png')

[英]place header('Content-Type: image/png') anywhere

I wanted to include the code having a header, 我想要包含带标题的代码,

is it possible to place 是否有可能放置

header('Content-Type: image/png')

anywhere in the code without getting error? 代码中的任何地方都没有出错

No. header() HAS to be before there is ANY output to the user. No. header()在用户有任何输出之前。

From the docs : 来自文档

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. 请记住,在发送任何实际输出之前,必须通过普通HTML标记,文件中的空行或PHP来调用header()。 It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. 使用include或require,函数或其他文件访问函数读取代码是一个非常常见的错误,并且在调用header()之前输出空格或空行。 The same problem exists when using a single PHP/HTML file. 使用单个PHP / HTML文件时存在同样的问题。

header() should be called before the output begins. 应在输出开始之前调用header()。 Please note there should not even white space be allowed to output to the browser before calling header(). 请注意,在调用header()之前,甚至不允许将空格输出到浏览器。 Else you will get an error. 否则你会收到错误。

'<?php
header('Content-type:image/png');

// Business logic here

?>'

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM