Home  >  Article  >  Backend Development  >  How to upload files in php and store them in mysql database_PHP tutorial

How to upload files in php and store them in mysql database_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:03:24825browse

How to upload files in php and store them in mysql database

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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

$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

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);

?>

7 8

9 10

11

13 14
$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);<🎜> <🎜>?>
Upload the file and save it to mysql, insert it through the insert statement ?
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); ?>
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/968669.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/968669.htmlTechArticleHow to upload files in php and store them in mysql database. This article mainly introduces how to upload files in php and store them in mysql database. method, a more detailed analysis of php operations in the form of complete examples...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn