简体   繁体   中英

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.

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. 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. The same problem exists when using a single PHP/HTML file.

header() should be called before the output begins. Please note there should not even white space be allowed to output to the browser before calling header(). Else you will get an error.

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

// Business logic here

?>'

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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