Php image binary conversion example

Source: Internet
Author: User
Here, we convert the uploaded file to binary when uploading the file and save it to the data field. we can display it in the same way next time we read it. Html: formactionquot; insertPic .... here, we convert the uploaded file to binary when uploading the file and save it to the data field. we can display it in the same way next time we read it.

Html:


Save the image to the database:

 Query ("insert into img (images) VALUES ('$ buf')"); // Obtain the number of affected rows if ($ result-> rowCount ()> 0) {echo ("data inserted. ");} Else {echo (" cannot be inserted. ") ;}// Explicitly close the PDO connection $ dbh = NULL;?>

Show Image: (show. php)

 

Or

1. fopen function.

The fopen () function is used to open a file or URL. Syntax:

Int fopen (string filename, string mode );

The mode parameter of the string can be the following:

The "r" file is read-only, and the file pointer points to the beginning.

The "r +" file opening method can be read/write, and the file pointer points to the beginning.

The "w" file opening mode is writing, the file pointer refers to the starting point, and the length of the original file is set to 0. If the file does not exist, create a new file.

The "w +" file opening method can be read/write, the file pointer points to the start, and the length of the original file is set to 0. If the file does not exist, create a new file.

"A" writes files, and the file pointer points to the end of the file. If the file does not exist, create a new file.

The "a +" file opening method can be read/write, and the file pointer points to the end of the file. If the file does not exist, create a new file.

"B" can be used if the operating system has different text and binary files. this parameter is not required for UNIX systems.

2. Addslashes function.

The Addslashes function is used to add a string to a diagonal line. Syntax: [Note: This function was successfully removed during the test. you know the truth. Try it and you will understand it]

String addslashes (string str );

This function adds a diagonal line to the quotation marks of the strings to be processed by the database, so that the database query can operate smoothly. The modified characters include single quotation marks ('), double quotation marks ("), backslash (), and NULL (the null byte ).

3. fread function.

The fread function is used to read the specified length of a bit group or to the end of the file EOF. Syntax:

String fread (int fp, int length); can be safely used for binary files

Fread () reads a maximum of length bytes from the file pointer. This function can be read by a maximum of length bytes, or when it reaches the EOF, or (for network streams) when a package is available, or (after opening the user space stream) when 8192 bytes have been read, the system stops reading files.

Convert binary to image

Note: $ newFilePath processes the generated image name and path. you can implement it here.

Export newfilepath+'1.jpg '; $ data = $ GLOBALS [HTTP_RAW_POST_DATA]; // Obtain the post-uploaded binary raw data if (empty ($ data) {www.phprm.com $ data = file_get_contents ("php: // input ") ;}$ newFile = fopen ($ newFilePath," w "); // open the file and write it into fwrite ($ newFile, $ data ); // write binary streams to the file fclose ($ newFile); // close the file

You can store the read binary stream to the database or directly write it into an image.

Obtain the binary header file to know what type of file it belongs.

 


Link to this article:

Add to favorites ^ please keep the tutorial address.

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.