Mega Code Archive

 
Categories / Php / MySQL Database
 

Here is a quick n dirty image uploader to a mysql db

Table structure: CREATE TABLE binary_data ( id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, description CHAR(50), bin_data LONGBLOB, filename CHAR(50), filesize CHAR(50), filetype CHAR(50) ); <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']))&&(isset($_FILES['form_data']))) { upload(); } else { show_form(); }//end if function upload() { //includes require("conn.php"); //define variables $image_file = ''; $image_desc = ''; $type = ''; $size = 0; $name = ''; $image_file = $_FILES['form_data']['tmp_name']; $image_desc = $_POST['form_description']; $type = $_FILES['form_data']['type']; $size = $_FILES['form_data']['size']; $name = $_FILES['form_data']['name']; $data = fread(fopen($image_file, "r"), filesize($image_file)); $data = addslashes($data); $result=connect("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ". "VALUES ('$image_desc','$data','$name','$size','$type')"); $id= mysql_insert_id(); print "<p>This file has the following Database ID: <b>$id</b>"; //show the form again to load the next image show_form(); } function show_form() { // else show the form to submit new data: ?> <form method="post" action="<?php echo $_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"> <input type="button" name="move" value="Show" onClick="javascript:window.location='show_desc.php';"> </form> <?php } ?> </body> </html>