daily pastebin goal
55%
SHARE
TWEET

Untitled

a guest Feb 18th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <form method="post" enctype="multipart/form-data" action="insert_image.php">
  2.     <input type="file" name="image" />
  3.     <input type="submit" />
  4. </form>
  5.    
  6. <?php
  7.  
  8. # getting the uploaded image and storing it
  9. if ( isset($_FILES['image']['tmp_name']) ) {
  10.     // open mysqli db connection
  11.     $mysqli = new mysqli($mysqliHost,$mysqliUsername,$mysqliPassword,$mysqliDatabase);
  12.  
  13.     // get image data
  14.     $binary = file_get_contents($_FILES['image']['tmp_name']);
  15.  
  16.     // get mime type
  17.     $finfo = new finfo(FILEINFO_MIME);
  18.     $type = $finfo->file($_FILES['image']['tmp_name']);
  19.     $mime = substr($type, 0, strpos($type, ';'));
  20.  
  21.     $query = "INSERT INTO `images`
  22.                     (`data`,`mime`,`name`)
  23.     VALUES('".$mysqli->real_escape_string($binary)."',
  24.             '".$mysqli->real_escape_string($mime)."',
  25.             '".$mysqli->real_escape_string($_FILES['image']['name'])."')";
  26.     $mysqli->query($query);
  27. }
  28.  
  29. # viewing the uploaded image
  30. if ( isset($_GET['imageName']) ) {
  31.     // open mysqli db connection
  32.     $mysqli = new mysqli($mysqliHost,$mysqliUsername,$mysqliPassword,$mysqliDatabase);
  33.  
  34.     // query for the image in the db
  35.     $query = "SELECT `data`,`mime` FROM `images` WHERE `name`='".$mysqli->real_escape_string($_GET['imageName'])."'";
  36.     $result = $mysql->query($query);
  37.  
  38.  
  39.     if ( $result->num_rows ) {
  40.         // grab the query result from the db select
  41.         $assoc = $result->fetch_assoc();
  42.  
  43.         // let the client browser know what type of data you're sending
  44.         header('Content-type: '.$assoc['mime']);
  45.  
  46.         // dump the binary data to the browser
  47.         echo $assoc['data'];
  48.         exit;
  49.     } else {
  50.         header('HTTP/1.1 404 Not Found');
  51.         exit;
  52.     }
  53. }
  54.  
  55. ?>
  56.    
  57. CREATE TABLE `images` (
  58.   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  59.   `data` longblob NOT NULL,
  60.   `mime` varchar(50) NOT NULL,
  61.   `name` varchar(255) NOT NULL,
  62.   PRIMARY KEY (`id`),
  63.   KEY `name` (`name`)
  64. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
  65.    
  66. $image = file_get_contents($image_file_name);
  67. $query = "INSERT INTO img SET image = "".mysqli_real_escape_string($image).""";
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top