PHP将图片存入数据库

我要做一个实现照片上传到数据库的功能。总感觉下面那段从上传目录读取写入数据库那段有错误。但不知道错在哪里。麻烦各位大大帮我看下。
<?php if(is_uploaded_file($_FILES['fileup']['tmp_name'])){
$upfile=$_FILES['fileup'];
$name=$upfile['name'];
$type=$upfile['type'];
$size=$upfile['size'];
$tmp_name=$upfile['tmp_name'];
$error=$upfile["error"];
//echo $tmp;
switch($type){
case 'image/bmp': $ok =1; break;
case 'image/pjpeg': $ok =1; break;
case 'image/gif': $ok =1; break;
case 'image/png': $ok =1; break;
case 'image/zip': $ok =1; break; } if($ok&&$error=='0'){ move_uploaded_file($tmp_name,'upload/'.$name); //print_r($_FILES['fileup']);
echo "成功上传!"."<br />";
} }
if ($_FILES["fileup"]["error"] > 0) {
echo "错误代码: " . $_FILES["fileup"]["error"] . "<br />"; }
else {
echo "上传: " . $_FILES["fileup"]["name"] . "<br />"; echo "类型: " . $_FILES["fileup"]["type"] . "<br />";
echo "大小: " . ($_FILES["fileup"]["size"] / 1024) . " Kb<br />";
echo "储存路径: " . "upload/" . $_FILES["fileup"]["name"]; }
?>
<?php include("conn.php");
$title=$_POST["title"];
$link=$_POST["link"];
$imgurl=$_POST["ImgUrl"];
if($_POST["botton"]){
$sql="INSERT INTO `bannerimg`(`link`, `ImgUrl`, `title`) VALUES ('$link','$imgurl','$title')"; if(mysql_query($sql))
echo "<script>alert('插入成功!')</script>";
else{ echo "<script>alert('插入失败!')</script>";
} } ?>
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head>
<body>
<form id="form1" name="form1" method="post" action="uploade.php"> <p> 图片标题: <label for="title"></label> <input type="text" name="title" id="title" /> </p>
<p>链接地址: <label for="link"></label> <input type="text" name="link" id="link" /> </p>
<p> <input type="image" name="imageField" id="imageField" src="<?php echo 'upload/'.$name ?>" width="1000px" height="206px"/> <input type="hidden" name="ImgUrl" id="ImgUrl" value="<?php echo 'upload/'.$name ?>" /> </p>
<p> <input type="submit" name="button" id="button" value="提交" /> </p>
</form>
</body>
</html>

插入图片和一般的数据没什么不同的,一般数据会了,传图片时候就用个move_uploaded_file改变下参数,主要是做这个的时候不要有负担
以下供参考
<?

function upload_file($files,$folder)//上传图片
{
$file_tyle = $files['type'];
$file_type_arr = array('image/gif','image/x-png','image/jpg','image/pjpeg');
if(!in_array($file_tyle,$file_type_arr) )
{
exit('file type only can be: png,jpeg,jpg,gif');
}
$knamearray = explode(".",$files["name"]);
$kname = $knamearray[count($knamearray)-1];
$rand_str = date("ymdhis");
$file_name = $rand_str.".".$kname;
$savepath = "$folder/";
/*$savepath = "$folder/date_".date('YmdHis')."/";
if( !is_dir($savepath) ) mkdir($savepath);*/
$upfile = $savepath.$file_name;
if( !move_uploaded_file($files['tmp_name'],$upfile) )
{
exit('upload error, please check your file type: png,jpeg,jpg,gif');
}
return $file_name;//不要回传值此行可注释掉
}
?>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-05-23
你可以把上面的php文件写成一个方法啊!!这么乱没几个人会认真看的!!

下面是我的一个图片上传函数你参考一下!!!!!

/***********************************
图片上传函数
***********************************/
function uploadFile($filename){
global $db;
global $config;
$attachment_dir = $config['attachment_dir'].date('Ym')."/";
!is_dir(ROOT_PATH.$attachment_dir)&&mkdir(ROOT_PATH.$attachment_dir);
$AllowedExtensions = array('bmp','gif','jpeg','jpg','png','doc','rar','docx','pdf');
$Extensions = end(explode(".",$_FILES[$filename]['name']));

if(!in_array(strtolower($Extensions),$AllowedExtensions)){
exit("<script>alert('格式错误!只支持后缀名为bmp,gif,jpeg,jpg,png,doc,rar,docx,pdf 的文件');window.history.go(-1)</script>");
}

$file_name = date('YmdHis').'_'.rand(10,99).'.'.$Extensions;
$upload_file = $attachment_dir.$file_name;
$upload_absolute_file = ROOT_PATH.$upload_file;
if (move_uploaded_file($_FILES[$filename]['tmp_name'], $upload_absolute_file)) {
$record = array(
'filename' =>$file_name,
'ffilename' =>$_FILES [$filename]['name'],
'path' =>$upload_file,
'ext' =>$Extensions,
'size' =>$_FILES [$filename]['size'],
'upload_date' =>date("Y-m-d H:i:s")
);
$id = $db->insert('cms_file',$record);
return $upload_file;
}
}
第2个回答  2012-05-27
不看代码 麻烦 建议你直接保存图片路径 图片存到文件夹里面调用的时候直接调用路径就可以了
第3个回答  2012-05-23
确实学的很乱。。。。看的晕头转向
要有一个好的书写习惯规范啊。。。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网