How to access binary files in MySQL

Source: Internet
Author: User

How to access the binary file, with the following code description

First create the test table TestTable
CREATE TABLE testtable (ID INT (5) Not NULL auto_increment PRIMARY key,filename CHAR (255), data Longblob);

Save a file in a table
<?php
mysql_connect ("localhost", "root", "password"); Connecting to a database
mysql_select_db ("database"); Selected database
$filename = ""//here fill in the binary file name
$data = Addslashes (Fread (fopen ($filename, "R"), FileSize ($filename));//Open file and normalize data into variable $data

$result =mysql_query ("INSERT into TestTable (filename,data) VALUES (' $filename ', ' $data ')")//data inserted into database test table

Mysql_close ();
?>

Retrieving files from a table
<?php


if ($id) {

mysql_connect ("localhost", "root", "password");

mysql_select_db ("database");
$filename = ""//here fill in the binary file name

$query = "Select data from testtable where Filename= $filename";
$result = mysql_query ($query);

$data = mysql_result ($result, 0, "data");

?>

Here to pay attention to is that PHP generally only support less than 2M of files, if you want to access more than 2M of files, it is necessary to enter the system settings.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.