PHP implements the coexistence of uploaded files into the database,

Source: Internet
Author: User
Tags mysql connect

PHP implements the coexistence of uploaded files into the database,

This example describes how PHP can upload files to the database. 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 "

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 "Upload successful ---"; echo "<a href ='' show _ info. php? Id = $ id ''> display the information of the uploaded file </a>";} else {echo "You have not uploaded any file" ;}?>

The upload. php file is as follows:

 

<Html> Reprinted from: http://aspnetjia.com

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.