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

发布时间:2019-10-07编辑:脚本学堂
php如何上传文件到数据库中,这里分享几个例子,掌握下php将文件保存到mysql数据库中的方法,怎么让php上传文件并存进数据库的实例代码。

php上传文件到数据库

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

1,修改php.ini
 

post_max_size
upload_max_filesize
 

2个参数的值,使他们大于你需要上传文件的大小

2,修改my.cnf
修改mysql/ target=_blank class=infotextkey>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("<b>请求错误: " . mysql_error() . "</b>");
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
<img src='image.php?img=1' border="0" alt="">

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("<b>error performing query: " . mysql_error() . "</b>");
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..)
 

<html>
<body>
..
<img src='image.php?img=1' border="0" alt="">
..
</body>
</html>

二、怎么让php 上传文件并存进数据库

1、show_info.php
 

<?php
if(!isset($_request['id']) or $_request['id']=="") die("error: id none");
$id = $_request['id'];
//定位记录,读出
$conn=mysql_connect("localhost","root","admin");
if(!$conn) die("error: mysql connect failed");
mysql_select_db("nokiapaymentplat",$conn);
$sql = "select * from receive where id=$id";
$result = mysql_query($sql,$conn);
if(!$result) die("error: mysql query");

$num=mysql_num_rows($result);
if($num<1) die("error: no this recorder");

$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
 

<?php
if(!isset($_request['id']) or $_request['id']=="") die("error: id none");
$id = $_request['id'];
//定位记录,读出
$conn=mysql_connect("localhost","root","admin");
if(!$conn) die("error: mysql connect failed");
mysql_select_db("nokiapaymentplat",$conn);

$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<1) die("error: no this recorder");

//下面两句程序也可以这么写
//$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 "<hr>上传的文件的信息:";
echo "<br>the file's name - $name";
echo "<br>the file's size - $size";
echo "<br><a href=show_add.php?id=$id>附件</a>";
?>

3、submit.php
 

<?php
if(is_uploaded_file($_files['myfile']['tmp_name'])) { //有了上传文件了

$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 "<a href='show_info.php?id=$id'>显示上传文件信息</a>";
}
else {
echo "你没有上传任何文件";
}
?>

4、upload.php
 

<html>
<head>
<title>文件上传表单</title>
</head>
<body>
<table>
<form enctype='multipart/form-data' name='myform' action='submit.php'
method='post'>
<input type = "hidden" name = "max_file_size" value ="1000000">
<tr><td>选择上传文件</td><td>
<input name='myfile' type='file'></td></tr>
<tr><td colspan='2'><input name='submit' value='上传' type='submit'></td></tr>
</table>
</body>
</html>