您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php文件上传 php上传文件到数据库

2024/10/29 3:54:18发布33次查看
php如何上传文件到数据库中,这里分享几个例子,掌握下php将文件保存到mysql数据库中的方法,怎么让php上传文件并存进数据库的实例代码。php上传文件到数据库无非是在数据库中建一个longblob字段来保存这个文件不过如果上传4--5m的文件,这个时候就会有些问题要注意
1,修改php.ini
post_max_sizeupload_max_filesize 2个参数的值,使他们大于你需要上传文件的大小
2,修改my.cnf修改mysql数据库的max_allowed_packet参数的值 该参数意义:max_allowed_packet一个包的最大尺寸。消息缓冲区被初始化为net_buffer_length字节,但是可在需要时增加到max_allowed_packet个字节。缺省地,该值太小必能捕捉大的(可能错误)包。如果你正在使用大的blob列,你必须增加该值。它应该象你想要使用的最大blob的那么大。
一、php把图片上传到数据库
建立3个php文件:readdir.php - 把图片放到数据库的代码image.php - 显示实际图片的代码view.php - 显示你如何调用数据库中的图片的代码
1,创建一个数据库
create table `images` (`imgid` int not null auto_increment ,`sixfourdata` longtext not null ,primary key ( `imgid` ) );readdir.php
具体内容:
$dbcnx = mysql_connect(localhost, username, password); mysql_select_db(base64imgdb);?>'需要打开一个目录 ./'readdir.php 文件定位于这个目录: $path = ./;$dir_handle = opendir($path) or die(unable to open directory $path);把图象分类,并且读出正在使用的一些数据 fopen'转换 base64_encode' 插入到表里
while ($file = readdir($dir_handle)) {$filetyp = substr($file, -3);if ($filetyp == 'gif' or $filetyp == 'jpg') {$handle = fopen($path . / . $file,'r');$file_content = fread($handle,filesize($path . / . $file));fclose($handle);$encoded = chunk_split(base64_encode($file_content)); $sql = insert into images set sixfourdata='$encoded'; mysql_query($sql);}}?>关闭设置的目录,然后处理:
closedir($dir_handle);echo(complete);mysql_close($dbcnx);?>读出图片的代码:image.php
$dbcnx = mysql_connect(localhost, username, password); mysql_select_db(base64imgdb);?>读出图片使用的代码image.php?img=x:
$img = $_request[img];?>之后需要连接数据库,然后读出:
$result = mysql_query(select * from images where imgid= . $img . ); if (!$result) { echo(请求错误: . mysql_error() . ); exit(); } while ($row = mysql_fetch_array($result)) { $imgid = $row[imgid];$encodeddata = $row[sixfourdata]; }?>
mysql_close($dbcnx);echo base64_decode($encodeddata);?>
理解base64-encoded 图象数据格式。让我们来看看具体的图片吧! view.php image.php?img=1image.php?img=357
readdir.php:
################################ db connection# change these values ###############################$dbcnx = mysql_connect(localhost, username, password); mysql_select_db(base64imgdb);
$path = ./;$dir_handle = opendir($path) or die(unable to open directory $path);while ($file = readdir($dir_handle)) {$filetyp = substr($file, -3);if ($filetyp == 'gif' or $filetyp == 'jpg') {$handle = fopen($file,'r');$file_content = fread($handle,filesize($file));fclose($handle);$encoded = chunk_split(base64_encode($file_content)); $sql = insert into images set sixfourdata='$encoded'; mysql_query($sql);}}
closedir($dir_handle);echo(complete);mysql_close($dbcnx);?>
image.php:
$dbcnx = mysql_connect(localhost, username, password);
mysql_select_db(base64imgdb);
$img = $_request[img];
$result = mysql_query(select * from images where imgid= . $img . );
if (!$result) {
echo(error performing query: . mysql_error() . ); exit(); } while ($row = mysql_fetch_array($result) ) { $imgid = $row[imgid];$encodeddata = $row[sixfourdata]; }mysql_close($dbcnx);echo base64_decode($encodeddata);?>
view.php (i shouldnt need to post this..)
....二、怎么让php 上传文件并存进数据库
1、show_info.php
2、show_info.php
name;//$size=$row->size;$name = mysql_result($result,0,file_name);$size = mysql_result($result,0,file_size);
mysql_close($conn);
echo
上传的文件的信息:;echo
the file's name - $name; echo
the file's size - $size; echo
附件;?>3、submit.php
4、upload.php
文件上传表单 选择上传文件
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product