PHP is usually used to generate a stream of HTML to be sent to the browser, but if you use the header function within your page you can use it to send any other type you wish. For example, if I include
header("Content-type: image/jpeg");
then I can script the production of images. Easy? Yes, but two words or caution:
a) The header call
must be included within your script before it generates a single byte of output. Rephrasing that, the < character of the initial <?php must be on the very first line and in the very first column of your script, and not a single byte may be output until you've made the header call.
b) If you select a format such as jpeg to send out, it's your responsibility to ensure that it's a valid jpeg image stream. More often than not, you'll do this with the Gd library, but if you're using PHP simply to upload images and manage their storage, that's not necessary. We have an example script to
upload, store and retrieve images in a database in our solution centre.
(written 2006-01-15, updated 2006-06-09)
Associated topics are indexed under
H112 - PHP - Further Web Page and Network Handling [4070] Passing variable between PHP pages - hidden fields, cookies and sessions - (2013-04-26)
[3918] Multiple page web applications - maintaining state - PHP - (2012-11-10)
[3568] Telling which ServerAlias your visitor used - useful during merging domains - (2012-01-04)
[3540] Easy session example in PHP - keeping each customers data apart - (2011-12-06)
[3432] 3 digit HTTP status codes - what are they, which are most common, which should be a concern? - (2011-09-11)
[3036] Sending out an email containing HTML from within a PHP page - (2010-11-07)
[2918] Downloading a report from the web for further local analysis - (2010-08-13)
[2729] Uploading a document or image to its own URL via a browser - (2010-04-18)
[2679] How to build a test harness into your PHP - (2010-03-16)
[2632] Shipping a test harness with your class in PHP - (2010-02-12)
[1549] http, https and ajp - comparison and choice - (2008-02-22)
[1518] Downloading data for use in Excel (from PHP / MySQL) - (2008-01-25)
[1515] Keeping staff up to date on hotel room status - (2008-01-22)
[1505] Script to present commonly used images - PHP - (2008-01-13)
[1496] PHP / Web 2 logging - (2008-01-06)
[1495] Single login and single threaded models - Java and PHP - (2008-01-04)
[1485] Copyright and theft of images, bandwidth and members. - (2007-12-26)
[1379] Simple page password protection - PHP - (2007-10-04)
[1355] .php or .html extension? Morally Static Pages - (2007-09-17)
[1210] PHP header() function - uses and new restrictions - (2007-05-30)
[1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
[1183] Improving searches - from OR to AND? - (2007-05-11)
[1114] PHP Image upload script - (2007-03-21)
[1009] Passing GET parameters through Apache mod_rewrite - (2006-12-27)
[936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
[904] Of course I'll tell you by email - (2006-10-25)
[847] Image maps for navigation - a straightforward example - (2006-08-28)
[789] Hot answers in PHP - (2006-07-02)
[767] Finding the language preference of a web site visitor - (2006-06-18)
[675] Adding PHP tags to an old cgi program - (2006-04-08)
[603] PHP - setting sort order with an associative array - (2006-02-13)
[542] Morning image, afternoon image - (2005-12-26)
[537] Daily Image Santafied - (2005-12-22)
[484] Setting the file name for a downloaded document - (2005-11-03)
[451] Accessing a page via POST from within a PHP script - (2005-09-26)
[443] Server side scripting of styles to suit the browser - (2005-09-12)
[425] Caching an XML feed - (2005-08-26)
[410] Reading a news or blog feed (RSS) in your PHP page - (2005-08-12)
[376] What brings people to my web site? - (2005-07-13)
[372] Time calculation in PHP - (2005-07-08)
[356] Sudoku helper or sudoku cheat - (2005-06-23)
[345] Spotting a denial of service attack - (2005-06-12)
[314] What language is this written in? - (2005-05-17)
[220] When to use Frames - (2005-02-19)
H118 - Generating CSS through PHP [996] Setting your colour theme through PHP - (2006-12-17)
[501] .css - using PHP to make dynamic style sheets - (2005-11-21)
W704 - Web and Intranet - Cascading style sheets - putting it all together [4037] Cascading Style Sheets and formatting your web page - (2013-03-10)
[2223] CSS Style Diagrams - working out where attributes come from - (2009-06-08)
[1677] CSS training - Cascading Style Sheets (UK course) - (2008-06-15)
[994] Training on Cascading Style Sheets - (2006-12-17)
5402
Some other Articles
Instructions for bright peopleNB ''Aspiration''Combining similar rows from a MySQL database selectMay all your screw-ups be big onesUsing PHP to output images, XML, Style sheets, etcOpen Source training from Well House ConsultantsMerging pictures using PHP and GDSmoke and mirrorsPython's Generator functionsThe fencepost problem