1

In my current code i only insert the filename of the file and the file is stored in a folder. I would also like to store the file in my mysqldatabase. How can i do that.

My table: id file_name fcontent (longblob)

include 'db.php';
if(isset($_FILES['image'])){
    $errors= array();
    $tablename = "files";
    $file_name = $_FILES['image']['name'];
    $file_size =$_FILES['image']['size'];

    $file_tmp =$_FILES['image']['tmp_name'];
    $content =$_FILES['image']['fcontent']; // content in database

    $sql="INSERT INTO $tablename(file_name,content)VALUES('" . mysql_real_escape_string($file_name) . "')"; // here i need to insert the content i assume

    $file_ext=strtolower(end(explode('.',$_FILES['image']['name']))); //convert to lower

$extensions = array("jpeg","jpg","png","txt","html","php","gif");   // File extension that are 

allowed 

if(in_array($file_ext,$extensions )=== false){ // check if value exists in array
     $errors[]="extension not allowed.";


    }
    if($file_size > 2097152){ // cant be greater than 2mb
    $errors[]='File size must be excately 2 MB';

    }               
    if(empty($errors)==true){
        mysql_query($sql);
        move_uploaded_file($file_tmp,"upload/".$file_name); 

        echo "Success";
        header("location:files.php"); // Send back to main page

    }else{
        print_r($errors);
 }

}
?>      
Ben
  • 41
  • 1
  • 1
  • 6
  • In general it's best practise to store the files in a folder and just store the names in the database. Is there a specific reason you need the file in the database? – Nicholas Smith Mar 25 '13 at 16:46
  • You start by **NOT** putting files in the db. there's very few usage cases that justify it, and the drawbacks are nasty. you also start by having proper upload handling code, and **NOT** assuming the upload was successful. – Marc B Mar 25 '13 at 16:46
  • Why do you want to store the files in the DB? In most all applications this is a bad idea. Considering you don't know how to do this at this time, I am wondering if you have also really thought out the reason for wanting to do this. – Mike Brant Mar 25 '13 at 16:46
  • @NicholasSmith Yes I also need to store them in the database – Ben Mar 25 '13 at 16:47
  • I did this quite a few years back and it caused a lot of headaches, especially when trying to do backups and restores via phpmyadmin – Waygood Mar 25 '13 at 16:48

4 Answers4

4

For my part, I would not recommend storing images in the database for these reasons: - Too heavy to manage. - Lack of speed (though many more images that is if they are heavy).

So I suggest you put the images in a directory, its properties (type, size, authors ..) and of course their link to the directory in the database,

Franky
  • 902
  • 2
  • 11
  • 28
  • What is the field type in the database in which you want to store the images?? – Franky Mar 25 '13 at 16:54
  • trying to follow this tutorial [link](http://forum.codecall.net/topic/40286-tutorial-storing-images-in-mysql-with-php/#sthash.kFu7uQAt.dpbs) I think it will help. – Franky Mar 25 '13 at 17:02
3

Currently I can think of two ways

1.use

BLOB       
MEDIUMBLOB 
LONGBLOB   

datatypes in mysql ... get contents using file_get_contents() and insert the contents

  1. use

      text
    

    datatype .. encode the contents and then save the contents..

Alternatively you can store just a path to the file ... like store http://domain/img.jpg in a table column

alwaysLearn
  • 6,882
  • 7
  • 39
  • 67
0

An example:

  $link = mysqli_connect("localhost", "root", "");
  mysqli_select_db($link, "mydb"); 

  $stmt = $link->prepare("insert into myfile (id, arq) values (?, ?)");

  $stmt->bind_param("ss", $id, $file);

  $id = 5;
  $file = file_get_contents("C:\\myfile.png");

  $stmt->execute();

If you wanna upload a file through some HTML form (enctype='multipart/form-data') then change the line

  $file = file_get_contents("C:\\myfile.png");

to

  $file = file_get_contents($_FILES['myFileName']['tmp_name']);

where myFileName in $_FILES['myFileName'] is the name of your input file html element.

mesompi
  • 659
  • 6
  • 12
-1

What is your problem? There are various topics about this question, one would be this

Community
  • 1
  • 1
kero
  • 10,647
  • 5
  • 41
  • 51
  • @kingkero My problem is that i cant get it to store the file in the database.Right now im only inserting filename but when i tried insert content it doesnt work – Ben Mar 25 '13 at 16:49