Home >Backend Development >PHP Tutorial >How to upload files in php and store them in mysql database_PHP tutorial
This article mainly introduces the method of uploading files in php and storing them in mysql database, with a more detailed analysis in the form of a complete example Learned the skills of PHP file uploading and database storage, which has certain reference value. Friends in need can refer to it
The example in this article describes how to upload files in php and store them in mysql database. Share it with everyone for your reference. The specific analysis is as follows:
The following codes are used to create mysql tables and upload files to save to mysql database
Create mysql table:
?
3 4
5
6
9 10 11 |
$con = mysql_connect("localhost", "", "");
mysql_select_db("w3m");
$sql = "CREATE TABLE updfiles ("
. "id INTEGER NOT NULL AUTO_INCREMENT"
<๐>. ", name VARCHAR(80) NOT NULL"<๐>
<๐>. ", type VARCHAR(80) NOT NULL"<๐>
<๐>. ", size INTEGER NOT NULL"<๐>
<๐>. ", content BLOB"<๐>
<๐>. ", PRIMARY KEY (id)"<๐>
<๐>. ")";<๐>
<๐>mysql_query($sql, $con);<๐>
<๐>mysql_close($con);<๐>
<๐>?>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <๐>$con = mysql_connect("localhost", "", "");<๐> <๐>mysql_select_db("w3m");<๐> <๐>$error = $_FILES['w3img']['error'];<๐> <๐>$tmp_name = $_FILES['w3img']['tmp_name'];<๐> <๐>$size = $_FILES['w3img']['size'];<๐> <๐>$name = $_FILES['w3img']['name'];<๐> <๐>$type = $_FILES['w3img']['type'];<๐> <๐>print("n");<๐> <๐>if ($error == UPLOAD_ERR_OK && $size > 0) { $fp = fopen($tmp_name, 'r'); $content = fread($fp, $size); fclose($fp); $content = addslashes($content); $sql = "INSERT INTO fyi_files (name, type, size, content)" . " VALUES ('$name', '$type', $size, '$content')"; mysql_query($sql, $con); print("File stored.n"); } else { print("Database Save for upload failed.n"); } print("n"); mysql_close($con); ?> |