Programming Resources

Uploading and viewing images with PHP and a mySQL database

How to upload an image into a mySQL database, and display the image from the database in a web page.

 

 

Database setup
The field to receive the image needs to of type BLOB or similar (depending on the maximum allowed file size of the image).

User input - specifying and uploading the file
Create a form within the web page, add a file field
<input name='myfile' type='file'>

The action will be a PHP file, and type will be 'POST'.



Processing the uploaded file
In upload.php, perform validation - check is an uploaded file, check mime type and file size.
Read the file, add slashes and insert into database with an id variable.

Retrieving the image
Create a PHP file named getimage.php
This script will retrieve the image specified by GET parameter id from the database.
It will then use the header statement to output an image header followed by the binary contents of the database field.

The script can be called as an image from other web pages
eg.

 

If you are looking for property in Piemonte Italy then we recommend alba property and piedmont property. Italian real estate.

Home > Programming Resources > PHP > Uploading and viewing images with PHP and a mySQL database

Contact

Telephone: 020 7193 2301
Email: webcontact@fearntech.co.uk

Full Contact Details >

 

© 2001-2008 Fearntech Limited. All rights reserved.
website design kent | seo kent | website maintenance
Valid XHTML | Valid CSS