willaguila Posted July 11, 2009 Share Posted July 11, 2009 Can anyone help me make sense of this? I'm trying to store images in a mysql database. I got the storing script working properly but the retrieval script below i don't know how to use to place the image on a web page? <?php //Shows the Picture showfile.php /*** some basic sanity checks ***/ if(filter_has_var(INPUT_GET, "image_id") !== false && filter_input(INPUT_GET, 'image_id', FILTER_VALIDATE_INT) !== false) { /*** assign the image id ***/ $image_id = filter_input(INPUT_GET, "image_id", FILTER_SANITIZE_NUMBER_INT); try { /*** connect to the database ***/ $dbh = new PDO("mysql:host=domain", 'dbase', 'password'); /*** set the PDO error mode to exception ***/ $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); /*** The sql statement ***/ $sql = "SELECT image, image_type FROM testblob WHERE image_id=$image_id"; /*** prepare the sql ***/ $stmt = $dbh->prepare($sql); /*** exceute the query ***/ $stmt->execute(); /*** set the fetch mode to associative array ***/ $stmt->setFetchMode(PDO::FETCH_ASSOC); /*** set the header for the image ***/ $array = $stmt->fetch(); /*** check we have a single image and type ***/ if(sizeof($array) == 2) { /*** set the headers and display the image ***/ header("Content-type: ".$array['image_type']); /*** output the image ***/ echo $array['image']; } else { throw new Exception("Out of bounds Error"); } } catch(PDOException $e) { echo $e->getMessage(); } catch(Exception $e) { echo $e->getMessage(); } } else { echo 'Please use a real id number'; } ?> <?php // View the pictures view.php /*** Check the $_GET variable ***/ if(filter_has_var(INPUT_GET, "image_id") !== false && filter_input(INPUT_GET, 'image_id', FILTER_VALIDATE_INT) !== false) { /*** set the image_id variable ***/ $image_id = filter_input(INPUT_GET, "image_id", FILTER_SANITIZE_NUMBER_INT); try { /*** connect to the database ***/ $dbh = new PDO("mysql:host=domain dbname=dbase", 'user', 'password'); /*** set the PDO error mode to exception ***/ $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); /*** The sql statement ***/ $sql = "SELECT image_type, image_size, image_name FROM testblob WHERE image_id=".$image_id; /*** prepare the sql ***/ $stmt = $dbh->prepare($sql); /*** exceute the query ***/ $stmt->execute(); /*** set the fetch mode to associative array ***/ $stmt->setFetchMode(PDO::FETCH_ASSOC); /*** set the header for the image ***/ $array = $stmt->fetch(); /*** the size of the array should be 3 (1 for each field) ***/ if(sizeof($array) === 3) { echo '<p>This is '.$array['image_name'].' from the database</p>'; echo '<img '.$array['image_size'].' src="showfile.php?image_id='.$image_id.'">'; } else { throw new Exception("Out of bounds error"); } } catch(PDOException $e) { echo $e->getMessage(); } catch(Exception $e) { echo $e->getMessage(); } } else { echo 'Please use a valid image id number'; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.