请问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 "保存完成";?> session的一些问题 php分页显示的问题。请教高手。急~!!在线等 \x7f-\xff疑惑 PHP中如何定义这样的变量在其他页面调用 为什么验证码能显示却取不到seesion 值 数据库中文乱码 网页正常 syre(神仙)进来领分,感谢回答PHP+Socket的应用,问题已经OK. "界面和代码脱离"的含义是什么。 100分,立即送:如何在新开全屏窗口的同时,关闭前一个窗口?? 谁能提供用 php 阅读和发送 E-Mail 完整代码。(2) 新手入门之一问(在线等。。。。) 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 "保存完成";
?>