This article mainly introduces how PHP uploads files and saves them to the database. it involves the file and form operations and database operations related skills involved in PHP file uploads, which is of great practical value, for more information about how to upload files to the database in PHP, see the example below. Share it with you for your reference. The details are as follows:
The show_add.php file is as follows:
<? Php if (! Isset ($ _ REQUEST ['id']) or $ _ REQUEST ['id'] = "") die ("error: id none "); $ id = $ _ REQUEST ['id']; // locate the record and read $ conn = mysql_connect ("localhost", "root", "admin"); if (! $ Conn) die ("error: mysql connect failed"); mysql_select_db ("nokiapaymentplat", $ conn); $ SQL = "select * from receive where id = $ id "; $ result = mysql_query ($ SQL, $ conn); if (! $ Result) die ("error: mysql query"); $ num = mysql_num_rows ($ result); if ($ num <1) die ("error: no this recorder "); $ data = mysql_result ($ result, 0, "file_data"); $ type = mysql_result ($ result, 0, "file_type"); $ name = mysql_result ($ result, 0, "file_name"); mysql_close ($ conn); // first output the corresponding file header and restore the original file name header ("Content-type: $ type "); header ("Content-Disposition: attachment; filename = $ name"); echo $ data;?>
The show_info.php file is as follows:
<? Php if (! Isset ($ _ REQUEST ['id']) or $ _ REQUEST ['id'] = "") die ("error: id none "); $ id = $ _ REQUEST ['id']; // locate the record and read $ conn = mysql_connect ("localhost", "root", "admin"); if (! $ Conn) die ("error: mysql connect failed"); mysql_select_db ("nokiapaymentplat", $ conn); $ SQL = "select file_name, file_size from receive where id = $ id "; $ result = mysql_query ($ SQL, $ conn); if (! $ Result) die ("error: mysql query"); // if no record is specified, an error is returned. $ num = mysql_num_rows ($ result); if ($ num <1) die ("error: no this recorder"); // you can write the following two statements in the program: // $ row = mysql_fetch_object ($ result ); // $ name = $ row-> name; // $ size = $ row-> size; $ name = mysql_result ($ result, 0, "file_name "); $ size = mysql_result ($ result, 0, "file_size"); mysql_close ($ conn); echo "information of the uploaded File:"; echo"
The file's name-$ name "; echo"
The file's size-$ size "; echo"
Attachment ";?>
The submit. php file is as follows:
<? Php if (is_uploaded_file ($ _ FILES ['myfile'] ['tmp _ name']) {// $ myfile = $ _ FILES ["myfile"] has been uploaded; // Set the timeout time. the default time is 30 seconds, $ time_limit = 60; set_time_limit ($ time_limit ); //// read the file content to the string $ fp = fopen ($ myfile ['tmp _ name'], "rb"); if (! $ Fp) die ("file open error"); $ file_data = addslashes (fread ($ fp, filesize ($ myfile ['tmp _ name']); fclose ($ fp); unlink ($ myfile ['tmp _ name']); // file format, name, size $ file_type = $ myfile ["type"]; $ file_name = $ myfile ["name"]; $ file_size = $ myfile ["size"]; die ($ file_type); // connect to the database, save the file to the database $ conn = mysql_connect ("localhost", "root", "admin"); if (! $ Conn) die ("error: mysql connect failed"); mysql_select_db ("nokiapaymentplat", $ conn); $ SQL = "insert into receive (file_data, file_type, file_name, file_size) values ('$ file_data', '$ file_type', '$ file_name', $ file_size) "; $ result = mysql_query ($ SQL, $ conn ); // the following sentence retrieves the insert statement id $ id = mysql_insert_id (); mysql_close ($ conn); set_time_limit (30 ); // restore default timeout setting echo "uploaded successfully ---"; echo "show uploaded file information" ;}else {echo "you did not Upload Any file ";}?>
The upload. php file is as follows:
Bytes
File upload form
I hope this article will help you with PHP programming.