-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);
 }

}
?>      
5
  • 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? Commented Mar 25, 2013 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. Commented Mar 25, 2013 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. Commented Mar 25, 2013 at 16:46
  • @NicholasSmith Yes I also need to store them in the database Commented Mar 25, 2013 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 Commented Mar 25, 2013 at 16:48

3 Answers 3

3

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,

Sign up to request clarification or add additional context in comments.

2 Comments

What is the field type in the database in which you want to store the images??
trying to follow this tutorial link I think it will help.
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

1 Comment

But personally I dont like storing images in database .. as it increasses the size manifold
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.