Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form action="upload.php" method="post" enctype="multipart/form-data">
- Select image to upload:
- <input type="file" name="image"/>
- <input type="submit" name="submit" value="UPLOAD"/>
- </form>
- <?php
- if(isset($_POST["submit"])){
- $check = getimagesize($_FILES["image"]["tmp_name"]);
- if($check !== false){
- $image = $_FILES['image']['tmp_name'];
- $imgContent = addslashes(file_get_contents($image));
- /*
- * Insert image data into database
- */
- //DB details
- $dbHost = 'localhost';
- $dbUsername = 'root';
- $dbPassword = '';
- $dbName = 'dictionary';
- //Create connection and select DB
- $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
- // Check connection
- if($db->connect_error){
- die("Connection failed: " . $db->connect_error);
- }
- $dataTime = date("Y-m-d H:i:s");
- //Insert image content into database
- $insert = $db->query("INSERT into images (image, created) VALUES ('$imgContent', '$dataTime')");
- if($insert){
- echo "File uploaded successfully.";
- }else{
- echo "File upload failed, please try again.";
- }
- }else{
- echo "Please select an image file to upload.";
- }
- }
- CREATE TABLE `images` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `image` longblob NOT NULL,
- `created` datetime NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Add Comment
Please, Sign In to add comment