Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form method="post" enctype="multipart/form-data" action="insert_image.php">
- <input type="file" name="image" />
- <input type="submit" />
- </form>
- <?php
- # getting the uploaded image and storing it
- if ( isset($_FILES['image']['tmp_name']) ) {
- // open mysqli db connection
- $mysqli = new mysqli($mysqliHost,$mysqliUsername,$mysqliPassword,$mysqliDatabase);
- // get image data
- $binary = file_get_contents($_FILES['image']['tmp_name']);
- // get mime type
- $finfo = new finfo(FILEINFO_MIME);
- $type = $finfo->file($_FILES['image']['tmp_name']);
- $mime = substr($type, 0, strpos($type, ';'));
- $query = "INSERT INTO `images`
- (`data`,`mime`,`name`)
- VALUES('".$mysqli->real_escape_string($binary)."',
- '".$mysqli->real_escape_string($mime)."',
- '".$mysqli->real_escape_string($_FILES['image']['name'])."')";
- $mysqli->query($query);
- }
- # viewing the uploaded image
- if ( isset($_GET['imageName']) ) {
- // open mysqli db connection
- $mysqli = new mysqli($mysqliHost,$mysqliUsername,$mysqliPassword,$mysqliDatabase);
- // query for the image in the db
- $query = "SELECT `data`,`mime` FROM `images` WHERE `name`='".$mysqli->real_escape_string($_GET['imageName'])."'";
- $result = $mysql->query($query);
- if ( $result->num_rows ) {
- // grab the query result from the db select
- $assoc = $result->fetch_assoc();
- // let the client browser know what type of data you're sending
- header('Content-type: '.$assoc['mime']);
- // dump the binary data to the browser
- echo $assoc['data'];
- exit;
- } else {
- header('HTTP/1.1 404 Not Found');
- exit;
- }
- }
- ?>
- CREATE TABLE `images` (
- `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
- `data` longblob NOT NULL,
- `mime` varchar(50) NOT NULL,
- `name` varchar(255) NOT NULL,
- PRIMARY KEY (`id`),
- KEY `name` (`name`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
- $image = file_get_contents($image_file_name);
- $query = "INSERT INTO img SET image = "".mysqli_real_escape_string($image).""";
Add Comment
Please, Sign In to add comment