Recent projects for special reasons, files need to be stored in a database. Today, specifically testing, the first to upload files on the PHP site, the file read-and then deposited into the MySQL database.
First, create the PHP code as follows (the web looked for a paragraph code has been modified): source code http://blog.csdn.net/jonathanlin2008/article/details/6185162
<?php/** * Created by Phpstorm. * User:yun * DATE:2015/7/10 * time:22:04 *///echo MD5 (' GGHSCQUI8EZIPSWV '); if ($_server[' request_method '] = = ' POST ') { $myfile =$_files[' myfile ']; /* Echo file_get_contents ($myfile [' tmp_name ']); exit;*/if ($myfile! = "None" && $myfile! = "") {//have uploaded file//Set timeout limit time, default time is 30 seconds, set to 0 for unlimited $time _limit=60; Set_time_limit ($time _limit); Name, size, file format, $file _name= $myfile [' name ']; $file _size= $myfile [' size ']; $file _type=strstr ($file _name, '. '); Read the contents of the file into a temporary file that is read directly in the string, because you do not need to place the file in the directory. $FP =fopen ($myfile [' Tmp_name '], "RB"); if (! $fp) Die ("File open error"); $file _data = addslashes (Fread ($fp, $file _size)); Fclose ($FP); /* $DBH = new PDO (' Mysql:host=192.168.1.168;port=3306;dbname=testdb; ', ' root ', ' 123 '); */* The following is a simple package for PDO */$u Serclass = DirName (__file__). '/source/module/user.php '; Require_once ($userclass); $user = new User (); The file field data type in the/*t_testfile table is longblod*/$sql = "INSERT INTO t_tEstfile (file) VALUES (' $file _data '); $result = $user->db->dosql ($sql); The following sentence takes out the ID of the INSERT statement just now $id = $user->db->getlastid (); echo "Upload successful---"; echo "<a href= ' show_info.php?id= $id ' > show upload file info </a>";} else {echo "You did not upload any files";}} /* Function Dosql ($sql, $model = ' many ', $debug = False) {if ($debug) echo $sql; $this->sth = $this->dbh->query ($sql); $this->getpdoerror (); $this->sth->setfetchmode (PDO::FETCH_ASSOC); if ($model = = ' many ') {$result = $this->sth->fetchall (); } else {$result = $this->sth->fetch (); } $this->sth = null; return $result;} */?><! DOCTYPE html>Second, encounter problems: file is larger than PHP default size is, upload get not file, directly reported "You did not upload any files"
So: Adjust the parameters in the php.ini configuration file and restart the Apache service.
Variable to adjust:
File_uploads = on; open File upload option
upload_max_filesize = 500M; upload file limit
If you want to upload larger files, only the above two are not enough, you must increase the server cache limit, the maximum execution time of the script is longer
Post_max_size = 500M;p ost Upper
max_execution_time = 1800; Maximum execution time of each script, in seconds script maximum execution times
max_input_time = 1800; Maximum amount of time each script may spend parsing request data
Memory_limit = 128M; Maximum amount of memories a script may consume (128MB) memory limit
Third, click Upload and there is a problem. MySQL server has gone away, because English is very dish, direct Youdao translates: ' MySQL servers have disappeared '
Baidu (or to thank Baidu, and the vast number of cattle people to share) let me find a solution, not lost for a long time.
Workaround: http://www.cnblogs.com/cenalulu/archive/2013/01/08/2850820.html
Php+mysql Accessing files