Html:
The code is as follows: |
Copy code |
<Form action = "insertPic. php" method = "post" enctype = "multipart/form-data" name = "mainForm" id = "mainForm"> <Input type = "file" name = "myFile"/> <Input type = "submit" name = "Submit" value = "Submit"/> </Form>
|
Save the image to the database:
The code is as follows: |
Copy code |
<? Php // Because the uploaded image is saved in a temporary file // You only need to read the file to obtain the uploaded image. $ Fp = fopen ($ _ FILES ["myFile"] ["tmp_name"], "rb "); $ Buf = addslashes (fread ($ fp, $ _ FILES ["myFile"] ["size"]); // Create a PDO object $ Dbh = new PDO ("mysql: host = localhost; port = 3306; dbname = test "," root "," 123456 & Prime ;); // Execute the insert operation and save the result in a variable $ Result = $ dbh-> query ("insert into img (images) VALUES ('$ buf ')"); // Obtain the number of affected rows If ($ result-> rowCount ()> 0 ){ Echo ("data inserted ."); } Else { Echo ("The INSERT operation cannot be performed ."); } // Explicitly close the PDO connection $ Dbh = NULL; ?> Show Image: (show. php) <? Php $ Conn = @ mysql_connect ("localhost", "root", "123456 & Prime;) or die (" server connection error !"); // Link to the database @ Mysql_select_db ("test", $ conn) or die ("no database found !"); $ Query = "select * from img where Id =". $ _ GET ['id']; $ Result = mysql_query ($ query); www.111cn.net $ Num = mysql_num_rows ($ result ); $ Data = mysql_result ($ result, 0, "images "); Header ("Content-type: image/". $ num ['imgtype']); Echo $ data; ?>
|
Or
The code is as follows: |
Copy code |
|
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 can be read and written, and the file pointer points to the beginning.
The "w" file opening mode is writing, the file pointer refers to the start point, and the length of the original file is set to 0. If the file does not exist, create a new file.
The "w +" open file 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" is used to write files, and the file pointer points to the end of the file. If the file does not exist, create a new file.
The "a +" open file 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's text and binary files are different. 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.
The code is as follows: |
Copy code |
Using newfilepath+'1.jpg '; $ Data = $ GLOBALS [HTTP_RAW_POST_DATA]; // Obtain the binary raw data from post. If (empty ($ data) {www.111cn.net $ Data = file_get_contents ("php: // input "); } $ NewFile = fopen ($ newFilePath, "w"); // open the file to prepare for writing Fwrite ($ newFile, $ data); // write binary streams to files 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.
The code is as follows: |
Copy code |
$ Bin = substr ($ content, 0, 2 ); $ StrInfo = @ unpack ("C2chars", $ bin ); $ TypeCode = intval ($ strInfo ['chars1']. $ strInfo ['chars2']); $ FileType = ''; Switch ($ typeCode) { Case 7790: $ FileType = 'exe '; Break; Case 7784: $ FileType = 'midi '; Break; Case 8297: $ FileType = 'rar '; Break; Case 255216: $ FileType = 'jpg '; Break; Case 7173: $ FileType = 'GIF '; Break; Case 6677: $ FileType = 'bmp '; Break; Case 13780: $ FileType = 'PNG '; Break; Default: Echo 'unknown '; } |