Database yang Dipilih Tidak Ditemukan"; $createNewDb = "BUAT DATABASE JIKA TIDAK ADA `PhpMysqlDatabaseBlobImageUpload`"; if (mysqli_query($conn, $createNewDb)) { echo "
Database Baru Berhasil Dibuat"; $selectCreatedDatabase = mysqli_select_db($conn, "PhpMysqlDatabaseBlobImageUpload"); if ($selectCreatedDatabase) { echo "
Dibuat Database Dipilih Berhasil"; // Membuat tabel baru $sqlcreatetable = " BUAT TABEL JIKA TIDAK ADA `imageuploadphpmysqlblob` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, `image` longblob NOT
Tabel Baru Dibuat"; } else { echo "
Tidak dapat membuat tabel baru. "; } } } else { echo "Tidak dapat membuat database"; } } if (isset($_POST['submit'])) { if (getimagesize($_FILES['imagefile']['tmp_name']) ==
Silakan Pilih Gambar. "; } else { //deklarasikan variabel $gambar = $_FILES['filegambar']['tmp_nama']; $nama = $_FILES['filegambar']['nama']; $gambar = base64_encode(file_get_contents(addslashes(
Gambar berhasil diunggah. "; } lain { gema "
Gambar Gagal diunggah
"; } } } else { # kode. } //Ambil gambar dari database dan tampilkan di halaman web html function displayImageFromDatabase(){ //gunakan kata kunci global untuk mendeklarasikan conn di dalam fungsi global $conn; . g. , gambar, file PDF, dan video, di database MySQL.
Sometimes, for security reasons, you may need to store large data objects, e.g., images, PDF files, and videos, in the MySQL database.
MySQL menyediakan tipe BLOB yang dapat menampung data dalam jumlah besar. BLOB adalah singkatan dari objek data besar biner. Nilai maksimum objek BLOB ditentukan oleh memori yang tersedia dan ukuran paket komunikasi. Anda dapat mengubah ukuran paket komunikasi menggunakan variabel
<?php /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }
Code language: PHP (php)0 di MySQL dan<?php /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }
Code language: PHP (php)1 di setelan PHPMari kita lihat bagaimana PHP PDO menangani tipe BLOB di MySQL
Pertama, kita buat tabel baru bernama
<?php /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }
Code language: PHP (php)_2 di database sampel untuk latihanTabel
<?php /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }
Code language: PHP (php)2 berisi tiga kolom- Kolom id adalah kunci utama, kolom penambahan otomatis
- Kolom mime menyimpan jenis file mime
- Kolom data yang tipe datanya adalah BLOB yang digunakan untuk menyimpan konten file
Pernyataan CREATE TABLE berikut membuat tabel
<?php /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }
Code language: PHP (php)2CREATE TABLE files ( id INT AUTO_INCREMENT PRIMARY KEY, mime VARCHAR (255) NOT NULL, data BLOB NOT NULL );
Code language: SQL (Structured Query Language) (sql)_Kedua, kita mendefinisikan kelas bernama
<?php /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }
Code language: PHP (php)_5 dengan kode berikut<?php /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }
Code language: PHP (php)_Dalam metode
<?php /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }
Code language: PHP (php)_6, kita membuka koneksi database ke database MySQL, dan dalam metode<?php /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }
Code language: PHP (php)7, kita menutup koneksiMasukkan data BLOB ke dalam database
PHP PDO menyediakan cara mudah untuk bekerja dengan data BLOB menggunakan aliran dan menyiapkan pernyataan. Untuk memasukkan konten file ke dalam kolom BLOB, ikuti langkah-langkah di bawah ini
- Pertama, buka file untuk dibaca dalam mode biner
- Kedua, buat pernyataan INSERT
- Ketiga, ikat pegangan file ke pernyataan yang disiapkan menggunakan metode
<?php /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }
Code language: PHP (php)8 dan panggil metode<?php /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }
Code language: PHP (php)9 untuk mengeksekusi kueri
Lihat metode
/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }
Code language: PHP (php)0 berikut/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }
Code language: PHP (php)Perhatikan bahwa
/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }
Code language: PHP (php)1 menginstruksikan PDO untuk memetakan data sebagai aliranPerbarui kolom BLOB yang ada
Untuk memperbarui kolom BLOB, Anda menggunakan teknik yang sama seperti yang dijelaskan dalam memasukkan data ke dalam kolom BLOB. Lihat metode
/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }
Code language: PHP (php)2 berikut/** * update the files table with the new blob from the file specified * by the filepath * @param int $id * @param string $filePath * @param string $mime * @return bool */ function updateBlob($id, $filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "UPDATE files SET mime = :mime, data = :data WHERE id = :id;"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); $stmt->bindParam(':id', $id); return $stmt->execute(); }
Code language: PHP (php)Kueri data dari kolom BLOB
Langkah-langkah berikut menjelaskan cara memilih data dari kolom BLOB
- Pertama, buat pernyataan SELECT
- Kedua, ikat parameter yang sesuai menggunakan metode
/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }
Code language: PHP (php)3 dari objek/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }
Code language: PHP (php)4 - Ketiga, jalankan pernyataan
Lihat metode
/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }
Code language: PHP (php)5 berikut/** * select data from the the files * @param int $id * @return array contains mime type and BLOB data */ public function selectBlob($id) { $sql = "SELECT mime, data FROM files WHERE id = :id;"; $stmt = $this->pdo->prepare($sql); $stmt->execute(array(":id" => $id)); $stmt->bindColumn(1, $mime); $stmt->bindColumn(2, $data, PDO::PARAM_LOB); $stmt->fetch(PDO::FETCH_BOUND); return array("mime" => $mime, "data" => $data); }
Code language: PHP (php)Contoh PHP MySQL BLOB
Dalam contoh berikut, kami akan menggunakan kelas
<?php /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }
Code language: PHP (php)_5 untuk menyimpan gambar GIF dan file PDF ke dalam kolom BLOB pada tabel<?php /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }
Code language: PHP (php)2PHP MySQL BLOB dengan file gambar
Pertama, kami memasukkan data biner dari file
/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }
Code language: PHP (php)_8 ke dalam kolom BLOB dari tabel<?php /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }
Code language: PHP (php)2 sebagai berikut$blobObj = new BlobDemo(); // test insert gif image $blobObj->insertBlob('images/php-mysql-blob.gif',"image/gif");
Kemudian, kita dapat memilih data BLOB dan menampilkannya sebagai gambar GIF.
$a = $blobObj->selectBlob(1); header("Content-Type:" . $a['mime']); echo $a['data'];
Code language: PHP (php)PHP MySQL BLOB dengan file PDF
Kode berikut menyisipkan konten file PDF
/** * update the files table with the new blob from the file specified * by the filepath * @param int $id * @param string $filePath * @param string $mime * @return bool */ function updateBlob($id, $filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "UPDATE files SET mime = :mime, data = :data WHERE id = :id;"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); $stmt->bindParam(':id', $id); return $stmt->execute(); }
Code language: PHP (php)0 ke dalam kolom BLOB$blobObj = new BlobDemo(); // test insert pdf $blobObj->insertBlob('pdf/php-mysql-blob.pdf',"application/pdf");
Code language: PHP (php)_Kemudian, kita dapat memilih data PDF dan merendernya di browser web sebagai berikut.
$a = $blobObj->selectBlob(2); header("Content-Type:" . $a['mime']); echo $a['data'];
Code language: PHP (php)Untuk mengganti file PDF dengan file gambar GIF, Anda menggunakan metode
/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }
Code language: PHP (php)2 sebagai berikut.$blobObj->updateBlob(2, 'images/php-mysql-blob.gif', "image/gif"); $a = $blobObj->selectBlob(2); header("Content-Type:" . $a['mime']); echo $a['data'];
Code language: PHP (php)Anda dapat mendownload source code tutorial ini melalui link berikut
Unduh Kode Sumber PHP MySQL BLOB
Dalam tutorial ini, kami telah menunjukkan kepada Anda cara mengelola data MySQL BLOB, termasuk menyisipkan, memperbarui, dan membuat kueri blob