简体   繁体   中英

How to store image into MySQL database?

I am using MySQL database along with PHP. How to implement a process to store an image in my database.

  1. You can store image in folder and name in database table with varchar data type

    Ex: if image, then filestoredpath/abc.png will be stored in table and abc.png will stored in folder

  2. image longblob data type in database table then take img as $img = file_get_contents($image); then insert into table with query "insert into images (image) values (?)";

For storing image to database , you can do like this :

  1. First upload the image in to you server.You can done this with this code

     $info = pathinfo($_FILES['userFile']['name']); $ext = $info['extension']; // get the extension of the file $newname = "newname.".rand(0,999).$ext; $target = 'images/'.$newname; move_uploaded_file( $_FILES['userFile']['tmp_name'], $target)
  2. Now store the image path that is in $target variable to you DB then you can get the image through the image path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM