MySQL是一個(gè)開放源碼的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),許多網(wǎng)站和應(yīng)用程序都使用它來存儲(chǔ)和處理數(shù)據(jù)。在MySQL中,插入附件是一項(xiàng)常見的任務(wù),因?yàn)樗梢宰屇爿p松地將文件添加到你的數(shù)據(jù)庫中。下面是如何在MySQL中插入附件的步驟:
步驟1:創(chuàng)建一個(gè)表來存儲(chǔ)附件和相關(guān)數(shù)據(jù)。 CREATE TABLE `attachments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `filename` varchar(255) NOT NULL, `mime_type` varchar(255) NOT NULL, `data` longblob NOT NULL, `created_at` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;步驟2:通過MySQL的LOAD方法將附件添加到數(shù)據(jù)庫中。 LOAD DATA INFILE 'path/to/file' INTO TABLE attachments FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' (filename, mime_type, data, created_at);步驟3:在MySQL中查詢附件并將其讀取。 SELECT filename, mime_type, data FROM attachments WHERE id = 1;步驟4:將附件下載到本地計(jì)算機(jī)。query($sql); $row = $result->fetch_assoc(); header("Content-type: {$row['mime_type']}"); header("Content-Disposition: attachment; filename=\"{$row['filename']}\""); echo $row['data']; ?>步驟5:在MySQL中刪除附件。 DELETE FROM attachments WHERE id = 1;