我做成了用户注册和留言板系统,就图片上传做不出,看了PHP手册,也不是很懂
解决方案 »
- php 大量图片 输入到数据库
- Fatal error: Maximum execution time of 60 seconds exceeded in
- eclipse PHP运行发生内部错误
- 关于linux 上传.php文件后,出现乱码问题
- 【新手问题】关于数组
- 一些积累了几天的问题:
- WINDOWS 2003 ENTERPRISE SERVER + PHP4.4 + MSSQL 2000 mssql_connet()如何连接不上?
- 第一菜鸟的session问题
- 运用文本数据库登陆的问题出错了,很郁闷.求助各位大哥大姐予以伸出援助之手!急救!我想给你们高分,但系统只有这么多,没有办法!
- 请问我能否实现在访问者访问我的网站的时候在他的机器上创建一个txt文件?
- 如何才能打印指定的地方呢(即打印局部)
- apache配置问题,url相关
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>upload_pic.htm</title>
<link href="51new.css" rel="stylesheet" type="text/css">
</head>
<body>
<script language="JavaScript">
//**********************************
//判断输入是否合法的JavaScript小程序
//**********************************
function check()
{
if (window.document.pic.name.value =="")
{
alert(" 请输入图片名称.");
document.pic.name.focus();
return false;
}
return true;
}
</script>
<table border="0" cellpadding="2" width="520" class="NT9">
<?php
//************************
// 注意,在form标签中需要加入代码:
// enctype="multipart/form-data",否则将
//不能实现图片文件的上传.
//***********************
?><form method="post" action="put_pic.php" name="pic"
enctype="multipart/form-data" onSubmit="return check();">
<tr>
<td width="520" bgcolor="#9FCF6F" colspan="2" >
上传图片</td>
</tr>
<tr>
<td width="160" bgcolor="#c2e0a5" class="NT9" align="right">图片名称</td>
<td width="360" class="NT9">
<input type="text" name="name" size="35">
</td>
</tr>
<tr>
<td width="160" bgcolor="#C2E0A5" class="NT9" align="right">图片说明:</td>
<td width="360" class="NT9">
<textarea cols=34 rows=8 name=info></textarea>
</td>
</tr>
<tr>
<td width="160" bgcolor="#c2e0a5" class="NT9" align="right">图片文件</td>
<td width="360" class="NT9">
<input type="file" name="pic" size="25">
</td>
</tr>
<tr>
<td width="520" class="NT9W" align="center" colspan="2">
<input type="submit" value="提交" name="submit">
<input type="reset" value="重写" name="B2">
</td>
</tr>
</form>
</table>
</body>
</html> show.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>show</title>
</head><body>
<?php
//连接服务器数据库
Connid=@mysql_connect("localhost","weizhou45c5011","dellhappy2003");
//************************
//在数据表$table中读取字段id的值
$sql="select pic from $_POST[photo] where id=$_POST[id]";
$result=mysql_db_query("table",$sql);
//读取图片文件内容
$image=mysql_result($result,0,"pic");
//显示图片文件
echo $_POST[image];
?>
</body>
</html> photo.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>photo</title><link href="51new.css" rel="stylesheet" type="text/css">
</head><body>
<table border="0" cellpadding="2" width="520" class="NT9" height="288">
<tr>
<td width="520" bgcolor="#9FCF6F" colspan="3" align="center" height="16">
相片集</td>
</tr>
<?php
//初始化分页变量$offset
if($offset=="")
{
$offset=0;
}
//连接服务器数据库
$Connid=@mysql_connect("localhost","weizhou45c5011","dellhappy2003");
//在数据表photo中按字段id逆序
//查询指定位置的6条记录的图片名称以及图片标识号等数据。
$sql="select id,name from photo order by id desc limit $offset,6";
$result=mysql_db_query("photo",$sql);$num=mysql_num_rows($result);
if ($num !=0)
{
//初始化表格换行变量$i
$i=0
//以while循环的方式依次显示所查询到的图片文件的图片和名称
while($r=mysql_fetch_array($result))
{
$id=$r["id"];
$ftitle=$r["name"];
if($i==0 or $i==3)
{
echo "<tr>";
}
echo"<td width=165 bgcolor=#C2E0A5 class=NT9 align=center height=126>";
//使用程序show.php来显示保存在数据表photo中的图片文件。
echo"<a href=photo_info.php?id=$fid>";
echo"<img border=0 src=";
echo"show.php?table=photo&id=$fid";
echo" height=60 ></a>";
//显示图片文件的名称
echo"<a href=photo_info.php?id=$fid>";
echo"<p>$_POST[ftitle]</p></a></td>";
if($i==2 or $i==5)
{
echo "<tr>";
}
$i++;
}
}
?>
</table>
</table>
</body>
</html>
mysql>CREATE TABLE photo
>(
>id int(10) NOT NULL auto_increment,
>filename varchar(30),
>filesize int(20),
>filetype varchar (10),
>time datetime,
>name varchar(50),
>info text,
>pic longblob,
>PRIMARY KEY (id)
>);
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>put_pic.</title>
</head>
<body>
<?php
//连接数据库
$Connid=@mysql_Connect("localhost","weizhou45c5011","dellhappy2003");
//将图片文件说明文字中的换行符转换为br符号
$info=nl2br($info);
//打开图片文件,将图片文件中的数据用函数addslashes处理,然后传递给变量$data,变量$data中所保存的数据就是图片文件的数据了。
if($pic !="none")
{
$fp=fopen($pic,"r");
$data=addslashes(fread($fp,filesize($pic)));
}
//获取图片文件的大小
$_POST[filesize]=filesize($pic);
//获取图片文件的类型
$_POST[filetype]=filetype($pic);
//获取图片文件的文件名
$_POST[filename]=basename($pic);
//将服务器的时间传递给变量$date
$date=date("Y-m-d H:i:s");
$query="insert into photo
(info,name,time,pic,filename,filesize,filetype)
values
('$_POST[info]','$_POST[name]','$_POST[date]','$_POST[data]',
'$_POST[filename]','$_POST[filesize]','$_POST[filetype]')";
if (mysql_db_query("photo",$query))
{
header("Location: photo.php");
}
?>
</body>
</html>
2、从show.php的$sql="select pic from $_POST[photo] where id=$_POST[id]";可以看出
你的php版本>4.1.0。所以上传的图片要用$_FILES数组访问
3、put_pic.php至少应为
if(isset($_FILES['pic'])) {
$fp=fopen($_FILES['pic']['tmp_name'],"r");
$data=addslashes(fread($fp,$_FILES['pic']['size'])); //获取图片文件的大小
$filesize = $_FILES['pic']['size'];
//获取图片文件的类型
$filetype = $_FILES['pic']['type'];
//获取图片文件的文件名
$filename = $_FILES['pic']['name'];
//将服务器的时间传递给变量$date
$date=date("Y-m-d H:i:s");
$query="insert into photo
(info,name,time,pic,filename,filesize,filetype)
values
('$_POST[info]','$_POST[name]','$date','$data',
'$filename','$filesize','$filetype')";