php上传文件并存储到mysql数据库的方法
程序员文章站
2022-05-13 22:29:42
本文实例讲述了php上传文件并存储到mysql数据库的方法。分享给大家供大家参考。具体分析如下:
下面的代码分别用于创建mysql表和上传文件保存到mysql数据库
创...
本文实例讲述了php上传文件并存储到mysql数据库的方法。分享给大家供大家参考。具体分析如下:
下面的代码分别用于创建mysql表和上传文件保存到mysql数据库
创建mysql表:
<?php $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); ?>
上传文件并保存到mysql中,通过insert语句插入
<?php $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); ?>
希望本文所述对大家的php程序设计有所帮助。
下一篇: 火热的天气,造成火热的段子