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

原创
2016-07-25 08:51:37 1697浏览
php如何上传文件到数据库中,这里分享几个例子,掌握下php将文件保存到mysql数据库中的方法,怎么让php上传文件并存进数据库的实例代码。

php上传文件到数据库 无非是在数据库中建一个longblob字段来保存这个文件 不过如果上传4--5m的文件,这个时候就会有些问题要注意

1,修改php.ini

post_max_size upload_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 . "//m.sbmmt.com/m/" . $file,'r'); $file_content = fread($handle,filesize($path . "//m.sbmmt.com/m/" . $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=1 image.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

$num=mysql_num_rows($result); if($num

$data = mysql_result($result,0,"file_data"); $type = mysql_result($result,0,"file_type"); $name = mysql_result($result,0,"file_name");

mysql_close($conn);

//先输出相应的文件头,并且恢复原来的文件名 header("content-type:$type"); header("content-disposition: attachment; filename=$name"); echo $data; ?>

2、show_info.php

$sql = "select file_name ,file_size from receive where id=$id"; $result = mysql_query($sql,$conn); if(!$result) die(" error: mysql query");

//如果没有指定的记录,则报错 $num=mysql_num_rows($result); if($num

//下面两句程序也可以这么写 //$row=mysql_fetch_object($result); //$name=$row->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

$myfile=$_files["myfile"];

//设置超时限制时间,缺省时间为 30秒,设置为0时为不限时 $time_limit=60; set_time_limit($time_limit); //

//把文件内容读到字符串中 $fp=fopen($myfile['tmp_name'], "rb"); if(!$fp) die("file open error"); $file_data = addslashes(fread($fp, filesize($myfile['tmp_name']))); fclose($fp); unlink($myfile['tmp_name']);

//文件格式,名字,大小 $file_type=$myfile["type"]; $file_name=$myfile["name"]; $file_size=$myfile["size"]; die($file_type); //连接数据库,把文件存到数据库中 $conn=mysql_connect("localhost","root","admin"); if(!$conn) die("error : mysql connect failed"); mysql_select_db("nokiapaymentplat",$conn);

$sql="insert into receive (file_data,file_type,file_name,file_size) values ('$file_data','$file_type','$file_name',$file_size)"; $result=mysql_query($sql,$conn);

//下面这句取出了刚才的insert语句的id $id=mysql_insert_id();

mysql_close($conn);

set_time_limit(30); //恢复缺省超时设置

echo "上传成功--- "; echo "显示上传文件信息"; } else { echo "你没有上传任何文件"; } ?>

4、upload.php

文件上传表单
选择上传文件


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。