Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <body>
- <form action="index.php" method="POST" enctype="multipart/form-data">
- Enter text:<br/>
- <input type="text" name="text" id="text"><br/><br/>
- Select image to upload:<br/>
- <input type="file" name="image" id="image"><br/><br/>
- <input type="submit" value="Send" name="submit">
- </form>
- </body>
- </html>
- <?php
- if(isset($_REQUEST["submit"])){
- $conn = dbConnect();
- if(!$conn)
- echo "Error with connection to database";
- $text = $_REQUEST["text"];
- if(isset($_FILES['image'])){
- $allowed_ext= array('jpg','jpeg','png','gif');
- $file_name =$_FILES['image']['name'];
- $file_ext = strtolower( end(explode('.',$file_name)));
- $file_size=$_FILES['image']['size'];
- $file_tmp= $_FILES['image']['tmp_name'];
- $type = pathinfo($file_name, PATHINFO_EXTENSION);
- $data = file_get_contents($file_tmp);
- $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
- if(in_array($file_ext,$allowed_ext) === false)
- {
- echo "Extension not allowed";
- }
- $image = $base64;
- }else{
- $image = null;
- }
- $stmt = $conn->prepare("INSERT INTO images(text,image) VALUES (?, ?)");
- $stmt->bind_param('ss', $text, $image);
- $stmt->execute();
- $stmt->close();
- $conn->close();
- echo "Data sent!";
- }
- function dbConnect()
- {
- $servername = "localhost";
- $username = "root";
- $password = "";
- // Create connection
- $conn = new mysqli($servername, $username, $password);
- // Check connection
- if ($conn->connect_error) {
- return false;
- }
- // Select database
- $conn->select_db("images");
- return $conn;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement