Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $user='<user>';
- $password='<pass>';
- $database='<db>';
- $host='localhost';
- $conn=mysql_connect($host,$user,$password) or die(mysql_error());
- mysql_select_db($database) or die(mysql_error());
- ?>
- <html>
- <head><title>Store binary data into SQL Database</title></head>
- <body>
- <?php
- // code that will be executed if the form has been submitted:
- if (isset($_POST['submit'])) {
- // connect to the database
- $tmp = $_FILES['form_data']['tmp_name'];
- $imgFormat = $_FILES['form_data']['type'];
- $fi = fopen($tmp,'r');
- $img = fread($fi,filesize($tmp));
- $img = addslashes($img);
- fclose($fi);
- $query = "INSERT INTO masti (caption, IMG) VALUES ('$imgFormat', '$img')";
- mysql_query($query) or die();
- $query = "SELECT img FROM masti WHERE id=1";
- $res = mysql_query($query) or die();
- $val = mysql_fetch_assoc($res);
- $frm = "Content-type: ".$imgFormat."";
- $Len = "Content-length: ".strlen($val['img'])."";
- header($Len);
- header($frm);
- echo $val['img'];
- exit();
- //echo "<img src='".$val['img']."' />";
- } else {
- // else show the form to submit new data:
- ?>
- <form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data">
- File Description:<br>
- <input type="text" name="form_description" size="40">
- <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
- <br>File to upload/store in database:<br>
- <input type="file" name="form_data" size="40">
- <p><input type="submit" name="submit" value="submit"></p>
- </form>
- <?php
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement