Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <form action="index.php" method="POST" enctype="multipart/form-data">  
  6.     Enter text:<br/>
  7.     <input type="text" name="text" id="text"><br/><br/>
  8.     Select image to upload:<br/>
  9.     <input type="file" name="image" id="image"><br/><br/>
  10.     <input type="submit" value="Send" name="submit">
  11. </form>
  12.  
  13. </body>
  14. </html>
  15.  
  16. <?php
  17.  
  18.  
  19.     if(isset($_REQUEST["submit"])){
  20.        
  21.         $conn = dbConnect();
  22.         if(!$conn)
  23.             echo "Error with connection to database";
  24.        
  25.         $text = $_REQUEST["text"];
  26.        
  27.         if(isset($_FILES['image'])){
  28.             $allowed_ext= array('jpg','jpeg','png','gif');
  29.             $file_name =$_FILES['image']['name'];
  30.             $file_ext = strtolower( end(explode('.',$file_name)));
  31.            
  32.             $file_size=$_FILES['image']['size'];
  33.             $file_tmp= $_FILES['image']['tmp_name'];
  34.  
  35.             $type = pathinfo($file_name, PATHINFO_EXTENSION);
  36.             $data = file_get_contents($file_tmp);
  37.             $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
  38.    
  39.             if(in_array($file_ext,$allowed_ext) === false)
  40.             {
  41.                 echo "Extension not allowed";
  42.             }
  43.            
  44.             $image = $base64;
  45.  
  46.         }else{
  47.             $image = null;
  48.         }
  49.        
  50.  
  51.         $stmt = $conn->prepare("INSERT INTO images(text,image) VALUES (?, ?)");
  52.         $stmt->bind_param('ss', $text, $image);
  53.         $stmt->execute();
  54.         $stmt->close();
  55.         $conn->close();
  56.        
  57.         echo "Data sent!";
  58.  
  59.     }
  60.    
  61.     function dbConnect()
  62.     {
  63.         $servername = "localhost";
  64.         $username = "root";
  65.         $password = "";
  66.  
  67.         // Create connection
  68.         $conn = new mysqli($servername, $username, $password);
  69.  
  70.         // Check connection
  71.         if ($conn->connect_error) {
  72.             return false;
  73.         }
  74.        
  75.         // Select database
  76.         $conn->select_db("images");
  77.        
  78.         return $conn;
  79.     }
  80.  
  81. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement