Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE pics (
- row_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
- name VARCHAR(30) NOT NULL,
- size INT NOT NULL,
- type VARCHAR(10) NOT NULL,
- content BLOB NOT NULL
- )
- <?php
- // Connect to database
- // Make a MySQL Connection
- mysql_connect("localhost","root","") or die(mysql_error());
- mysql_select_db("test") or die(mysql_error());
- // Insert any new image into database
- if(isset($_POST['xsubmit']) && $_FILES['imagefile']['name'] != "") {
- $fileName = $_FILES['imagefile']['name'];
- $fileSize = $_FILES['imagefile']['size'];
- $fileType = $_FILES['imagefile']['type'];
- $content = addslashes (file_get_contents($_FILES['imagefile']['tmp_name']));
- if(!get_magic_quotes_gpc()) {
- $fileName = addslashes($fileName);
- }
- // Checking file size
- if ($fileSize < 150000) {
- mysql_query ("insert into pics (name,size,type,content) "
- . "values ('$fileName','$fileSize','$fileType','$content')");
- } else {
- $err = "Too large!";
- }
- }
- // Find out about latest image
- $gotten = mysql_query("select * from pics order by row_id desc");
- $row = mysql_fetch_assoc($gotten);
- $bytes = $row['content'];
- // If this is the image request, send out the image
- if ($_REQUEST['pic'] == 1) {
- header("Content-type: $row[type];");
- print $bytes;
- }
- ?>
- <html>
- <head>
- <title>Upload an image to a database</title>
- </head>
- <body>
- <font color="#FF3333"><?php echo $err ?></font>
- <table>
- <form name="myform" enctype="multipart/form-data" method="post">
- <tr>
- <td>Upload <input type="file" name="imagefile">
- <input type="submit" name="xsubmit" value="Upload">
- </td>
- </tr>
- <tr>
- <td>Latest Image</td>
- </tr>
- <tr>
- <td><img src="?pic=1"></td>
- </tr>
- </form>
- </table>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment