Re: Loading images on MYSQL through a PHP web page

From: Date: Thu, 07 Apr 2005 02:27:56 +0000
Subject: Re: Loading images on MYSQL through a PHP web page
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-39144@lists.php.net to get a copy of this message
* Noel Simela <NoelS@orinoco.co.bw>: > Does anyone know how to load pictures through php to a mysql db...i want > users on my site to be able upload their pictures on the db,whenever i > view their profiles i should be able to see their pics After uploading the file (see http://php.net/features.file-upload), do something like: list($width, $height, $type, $attr) = getimagesize($filename); $type = image_type_to_mime_type($type); // image MIME type $fh = fopen($filename, 'r'); // use 'rb' if on a windows platform $image = fread($fh); // The following uses PEAR::DB for data abstraction; you can do // similarly using ADODB or using PHP's mysql functions $sql = "INSERT INTO table(image, mime_type) VALUES(?, ?)"; // using a placeholder $db->query($sql, array($image, $type)); // using PEAR::DB What this *doesn't* do is tell you what TYPE of image it is. Then, when you need to display the images, create a script that grabs the image and its MIME type from the database, and do the following: header("Content-Type: $type"); echo $image; It's important that you capture the MIME type of the image -- that way you can report it to the browser so that it can properly render the image. -- Matthew Weier O'Phinney | WEBSITES: Webmaster and IT Specialist | http://www.garden.org National Gardening Association | http://www.kidsgardening.com 802-863-5251 x156 | http://nationalgardenmonth.org mailto:matthew@garden.org | http://vermontbotanical.org

« previous php.db (#39144) next »