请问php中怎样把图片写到mysql数据库中?或者存到某个文件夹中 把图片的路径存到 mysql中,把图片传到相应的路径下面! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ccterran(iwind) 的代码简洁明了。推荐。http://expert.csdn.net/Expert/topic/2977/2977700.xml?temp=.0338251存到目录的看看$_FILES[]和COPY用法就行了。 上传后将文件名写入数据库的例子,假设表名pic,字段id,picname,pic1.php<form action="2.php" method="post" enctype="multipart/form-data"><table width="98%" border="0" cellspacing="1" cellpadding="0"> <tr align="left"> <td colspan="2"><? echo $_GET['message'];?></td> </tr> <tr> <td width="25%" align="right">名称</td> <td width="75%" align="left"><input name="name" type="text" id="name"></td> </tr> <tr> <td align="right">图片</td> <td align="left"><input type="file" name="image"></td> </tr> <tr> <td align="right"> </td> <td align="left"><input type="submit" name="Submit" value="提交"></td> </tr> <tr> <td align="right"> </td> <td align="left"> </td> </tr></table></form>2.php<?//函数,用于产生id;function makekey(){ $a=array(" ","."); $tf=str_replace($a,"",microtime()); return $tf;}//连数据库。$hostname_myconn = "localhost";$database_myconn = "pic";$username_myconn = "root";$password_myconn = "";$myconn = mysql_connect($hostname_myconn, $username_myconn, $password_myconn);mysql_select_db($database_myconn);//开始处理 $name=$_POST['name']; if (empty($name)) { header("location:1.php?message=请输入名称"); exit; } $image=$_FILES['image']['name']; $size=$_FILES['image']['size']; $type=$_FILES['image']['type']; //检查文件类型及大小。 if( ($type == "image/gif" || $type == "image/png" || $type == "image/pjpeg")&& $size<200000) { $a=array(" ","."); //产生文件名 $tf=str_replace($a,"",microtime()).".".substr($image,-3); //文件保存在pic子目录下 $dest_image = "pic/".$tf; if (!move_uploaded_file($_FILES['image']['tmp_name'], $dest_image)) { $tf = "no"; } } else { header("location:1.php?message=请选择文件上传,或者你选择的文件不符合要求"); exit; } $id=makekey(); //存入数据库 $str="insert into pic (id,picname,pic) values ('$id','$name','$tf')"; mysql_query($str); //显示信息或重定向页面 echo "保存完成";?> LAST_INSERT_ID() 到底可靠吗? 咳,我们国家计算机发展水平实在是太次了!! 关于mysql插入信息的问题 很简单 逐条更新MySQL记录,但是实现的时候,前后台数据不一致 大家帮忙看看,Mysqli使用有问题 php滚动条函数! 求缩略图代码 preg_replace匹配尖括号的问题 pdo 访问SQLSERVER存储过程无法获取结果集 thinkphp写注册 新手入门之一问(在线等。。。。) Session问题
http://expert.csdn.net/Expert/topic/2977/2977700.xml?temp=.0338251
存到目录的看看$_FILES[]和COPY用法就行了。
1.php
<form action="2.php" method="post" enctype="multipart/form-data">
<table width="98%" border="0" cellspacing="1" cellpadding="0">
<tr align="left">
<td colspan="2"><? echo $_GET['message'];?></td>
</tr>
<tr>
<td width="25%" align="right">名称</td>
<td width="75%" align="left"><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td align="right">图片</td>
<td align="left"><input type="file" name="image"></td>
</tr>
<tr>
<td align="right"> </td>
<td align="left"><input type="submit" name="Submit" value="提交"></td>
</tr>
<tr>
<td align="right"> </td>
<td align="left"> </td>
</tr>
</table>
</form>2.php
<?
//函数,用于产生id;
function makekey()
{
$a=array(" ",".");
$tf=str_replace($a,"",microtime());
return $tf;
}
//连数据库。
$hostname_myconn = "localhost";
$database_myconn = "pic";
$username_myconn = "root";
$password_myconn = "";
$myconn = mysql_connect($hostname_myconn, $username_myconn, $password_myconn);
mysql_select_db($database_myconn);
//开始处理
$name=$_POST['name'];
if (empty($name))
{
header("location:1.php?message=请输入名称");
exit;
}
$image=$_FILES['image']['name'];
$size=$_FILES['image']['size'];
$type=$_FILES['image']['type'];
//检查文件类型及大小。
if( ($type == "image/gif" || $type == "image/png" || $type == "image/pjpeg")&& $size<200000)
{
$a=array(" ",".");
//产生文件名
$tf=str_replace($a,"",microtime()).".".substr($image,-3);
//文件保存在pic子目录下
$dest_image = "pic/".$tf;
if (!move_uploaded_file($_FILES['image']['tmp_name'], $dest_image))
{
$tf = "no";
}
}
else
{
header("location:1.php?message=请选择文件上传,或者你选择的文件不符合要求");
exit;
}
$id=makekey();
//存入数据库
$str="insert into pic (id,picname,pic) values ('$id','$name','$tf')";
mysql_query($str);
//显示信息或重定向页面
echo "保存完成";
?>