Example of storing php uploaded images to a database

Source: Internet
Author: User

Most people upload images by saving a path to the database, which is indeed fast during insertion and meets the characteristics of the web, but it is very troublesome to delete the file. You need to find and delete the file, this code can directly store the code into the database and delete it when it is deleted. Please note: In this case, the database size will surge, please use it as appropriate

Table Structure
Copy codeThe Code is as follows:
Create table 'upload '(
'Id' int (10) unsigned not null AUTO_INCREMENT,
'Type' varchar (20) not null,
'Data' mediumblob not null,
Primary key ('id ')
) ENGINE = InnoDB AUTO_INCREMENT = 1 default charset = utf8;

Index.html
Copy codeThe Code is as follows:
<! Doctype html>
<Html>
<Head>
<Title>
Post-Image
</Title>
</Head>
<Body>
<Form action = "post. php" method = "post" enctype = "multipart/form-data">
<Input type = "file" name = "file" id = "file"/>
<Input type = "submit" value = "OK"/>
</Form>
</Body>
</Html>

Post. php
Copy codeThe Code is as follows:
<? Php
If ($ _ FILES ["file"] ["error"]> 0)
{
Echo "Error:". $ _ FILES ["file"] ["error"]. "<br/> ";
}
Else
{
$ Type = $ _ FILES ["file"] ["type"];
$ Size = $ _ FILES ['file'] ['SIZE'];
$ Tmp = $ _ FILES ["file"] ["tmp_name"];
$ Fp = fopen ($ tmp, 'rb ');
$ Data = bin2hex (fread ($ fp, $ size ));
$ Dsn = 'mysql: host = localhost; dbname = test ';
Echo '<pre> ';
Try {
$ Pdo = new PDO ($ dsn, 'root', 'root ');
$ Pdo-> exec ("insert into 'upload' ('type', 'data') values ('$ type', 0x $ data )");
$ Id = $ pdo-> lastInsertId ();
Echo 'upload success! <A href = "view. php? Id = '. $ id.' "> View </a> ';
$ Pdo = null;
} Catch (PDOException $ e ){
Echo $ e-> getMessage ();
}
Echo '</pre> ';
Fclose ($ fp );
}

View. php
Copy codeThe Code is as follows:
<? Php
$ Id = $ _ GET ['id'];
If (is_numeric ($ id )){
$ Dsn = 'mysql: host = localhost; dbname = test ';
Try {
$ Pdo = new PDO ($ dsn, 'root', 'root ');
$ Rs = $ pdo-> query ('select * from 'upload' where 'id' = '. $ id );
$ Row = $ rs-> fetchAll ();
$ Data = $ row [0];
Header ("Content-Type :$ {data ['type']}");
Echo $ data ['data'];
$ Pdo = null;
} Catch (PDOException $ e ){
Echo $ e-> getMessage ();
}
} Else {
Exit ();
}

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.