Làm cách nào để chèn dữ liệu BLOB vào MySQL bằng PHP?

Chèn hành động bằng cách sử dụng load_file để tải nội dung blob từ tệp được chỉ định

INSERT INTO table_name [file_name,file_content] VALUES["data.txt",LOAD_FILE["d:/data.txt"]];
INSERT INTO table_name [file_name,file_content] VALUES["sample.txt",LOAD_FILE["d:/sample.txt"]];

Tạo một bảng để lưu trữ hình ảnh theo byte
Tạo Cơ sở dữ liệu với bảng cụ thể dưới dạng >> tạo hình ảnh cơ sở dữ liệu; >> tạo bảng lưu trữ hình ảnh [id int not null auto increment, primary key[id], name varchar[50] , image longblob , size int ];
Kết quả -------->>Bảng được tạo thành công

HTML và CSS được thêm vào bằng PHP
Lưu trữ tệp trong một tên. Tải ảnh lên. php


Upload - photo



Please Select a file'; } else { try { $msg = upload[]; // function calling to upload an image echo $msg; } catch[Exception $e] { echo $e->getMessage[]; echo 'Sorry, Could not upload file'; } } function upload[] { include "database/dbco.php"; $maxsize = 10000000; //set to approx 10 MB //check associated error code if[$_FILES['userfile']['error']==UPLOAD_ERR_OK] { //check whether file is uploaded with HTTP POST if[is_uploaded_file[$_FILES['userfile']['tmp_name']]] { //checks size of uploaded image on server side if[ $_FILES['userfile']['size'] < $maxsize] { $finfo = finfo_open[FILEINFO_MIME_TYPE]; //checks whether uploaded file is of image type if[strpos[finfo_file[$finfo, $_FILES['userfile']['tmp_name']],"image"]===0] { // prepare the image for insertion $imgData =addslashes [file_get_contents[$_FILES['userfile']['tmp_name']]]; // put the image in the db... // database connection $host = '127.0.0.1'; $user = 'root'; $pass = 'your_password'; $db = 'database_name'; mysql_connect[$host, $user, $pass] OR DIE [mysql_error[]]; // select the db mysql_select_db [$db] OR DIE ["Unable to select db".mysql_error[]]; // our sql query $sql = "INSERT INTO storeimages [id,image, name,size] VALUES ['$_POST[id]','{$imgData}', '{$_FILES['userfile']['name']}','{$_FILES['userfile']['size']}'];"; mysql_query[$sql] or die["Error in Query insert: " . mysql_error[]]; // insert the image $msg='

Image successfully saved in database .

'; } else $msg="

Uploaded file is not an image.

"; } else { // if the file is not less than the maximum allowed, print an error $msg='

File exceeds the Maximum File limit

Maximum File limit is '.$maxsize.' bytes

File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size']. ' bytes

'; } } else $msg="File not uploaded successfully."; } else { $msg= file_upload_error_message[$_FILES['userfile']['error']]; } return $msg; } // Function to return error message based on error code function file_upload_error_message[$error_code] { switch [$error_code] { case UPLOAD_ERR_INI_SIZE: return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; case UPLOAD_ERR_FORM_SIZE: return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; case UPLOAD_ERR_PARTIAL: return 'The uploaded file was only partially uploaded'; case UPLOAD_ERR_NO_FILE: return 'No file was uploaded'; case UPLOAD_ERR_NO_TMP_DIR: return 'Missing a temporary folder'; case UPLOAD_ERR_CANT_WRITE: return 'Failed to write file to disk'; case UPLOAD_ERR_EXTENSION: return 'File upload stopped by extension'; default: return 'Unknown upload error'; } } ?>


Tóm lược. trong hướng dẫn này, bạn sẽ học cách xử lý dữ liệu BLOB bằng PHP PDO. Chúng tôi sẽ chỉ cho bạn cách chèn, cập nhật và chọn dữ liệu BLOB trong cơ sở dữ liệu MySQL

Đôi khi, vì lý do bảo mật, bạn có thể cần lưu trữ các đối tượng dữ liệu lớn, chẳng hạn như. g. , hình ảnh, tệp PDF và video trong cơ sở dữ liệu MySQL

MySQL cung cấp loại BLOB có thể chứa một lượng lớn dữ liệu. BLOB là viết tắt của đối tượng dữ liệu lớn nhị phân. Giá trị tối đa của đối tượng BLOB được chỉ định bởi bộ nhớ khả dụng và kích thước gói giao tiếp. Bạn có thể thay đổi kích thước gói giao tiếp bằng cách sử dụng biến 

Chủ Đề