上传图片原理:首先判断文件类型是否为图片格式,若是则上传文件,然后重命名文件(一般都是避免上传文件重名,现在基本上都是以为时间来命名),接着把文件上传到指定目录,成功上传后输出上传图片的预览.
①首先我们开始判断文件类型是否为图片类型用到的函数
{
strrchr:查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符.
substr: 取部份字符串.
$HTTP_POST_FILES['file']['name']:获取当前上传的文件全称.
}
图片类型就是"."后面的字符(比如:一个文件名称为XXX.JPG 那么它的类型就是"."后面的JPG). 我们可以用PHP中的函数来截取上传者文件名字的.我们来写个获取文件类型的函数
function type()
return substr(strrchr($HTTP_POST_FILES['file']['name'],'.'),1);
php
$uptypes=array('image/jpg', //上传文件类型列表
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'application/x-shockwave-flash',
'image/x-png');
$destination_folder="upload/"; //上传文件路径
$watermark=0; //是否附加水印(1为加水印,其他为不加水印);
$waterstring="newphp.site.cz"; //水印字符串
$waterimg="xplore.gif"; //水印图片
$imgpreview=1; //是否生成预览图(1为生成,其他为不生成);
html
head
table.itable{}
/head
body
centerform enctype="multipart/form-data" method="post" name="upform"
上传文件: brbrbr
允许上传的文件类型为:jpg|jpeg|png|pjpeg|gif|bmp|x-png|swf brbr
a href="index.php"返回/a
/form
if ($_SERVER['REQUEST_METHOD'] == 'POST')
if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
//是否存在文件
echo "font color='red'文件不存在!/font";
exit;
$file = $_FILES["upfile"];
if($max_file_size $file["size"])
//检查文件大小
echo "font color='red'文件太大!/font";
if(!in_array($file["type"], $uptypes))
//检查文件类型
echo "font color='red'只能上传图像文件或Flash!/font";
if(!file_exists($destination_folder))
mkdir($destination_folder);
$filename=$file["tmp_name"];
$image_size = getimagesize($filename);
$pinfo=pathinfo($file["name"]);
$ftype=$pinfo[extension];
$destination = $destination_folder.time().".".$ftype;
if (file_exists($destination) $overwrite != true)
echo "font color='red'同名文件已经存在了!/a";
if(!move_uploaded_file ($filename, $destination))
echo "font color='red'移动文件出错!/a";
$pinfo=pathinfo($destination);
$fname=$pinfo[basename];
echo " font color=red已经成功上传/fontbr文件名: font color=blue".$destination_folder.$fname."/fontbr";
echo " 宽度:".$image_size[0];
echo " 长度:".$image_size[1];
if($watermark==1)
$iinfo=getimagesize($destination,$iinfo);
$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
$black=imagecolorallocate($nimage,0,0,0);
imagefill($nimage,0,0,$white);
case 1:
$simage =imagecreatefromgif($destination);
break;
$simage =imagecreatefromjpeg($destination);
$simage =imagecreatefrompng($destination);
$simage =imagecreatefromwbmp($destination);
default:
die("font color='red'不能上传此类型文件!/a");
imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
switch($watertype)
case 1: //加水印字符串
$simage1 =imagecreatefromgif("xplore.gif");
imagedestroy($simage1);
//imagegif($nimage, $destination);
imagejpeg($nimage, $destination);
imagepng($nimage, $destination);
imagewbmp($nimage, $destination);
//imagejpeg($nimage, $destination);
//覆盖原上传文件
imagedestroy($nimage);
imagedestroy($simage);
if($imgpreview==1)
echo "br图片预览:br";
echo "a href=\"".$destination."\" target='_blank'img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);
echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\" border='0'/a";
/center
/body
/html
php实现上传图片保存到数据库的方法.具体分析如下:
php 上传图片,一般都使用move_uploaded_file方法保存在服务器上.但如果一个网站有多台服务器,就需要把图片发布到所有的服务器上才能正常使用(使用图片服务器的除外)
如果把图片数据保存到数据库中,多台服务器间可以实现文件共享,节省空间.
首先图片文件是二进制数据,所以需要把二进制数据保存在mysql数据库.
mysql数据库提供了BLOB类型用于存储大量数据,BLOB是一个二进制对象,能容纳不同大小的数据.
BLOB类型有以下四种,除存储的最大信息量不同外,其他都是一样的.可根据需要使用不同的类型.
数据表photo,用于保存图片数据,结构如下:
CREATE?TABLE?◆photo◆?(?
◆id◆?int(10)?unsigned?NOT?NULL?auto_increment,?
◆type◆?varchar(100)?NOT?NULL,?
◆binarydata◆?mediumblob?NOT?NULL,?
PRIMARY?KEY?(◆id◆)?
)?ENGINE=MyISAM?DEFAULT?CHARSET=latin1?AUTO_INCREMENT=1?;
upload_image_todb.php代码如下:
php?
//?连接数据库?
$conn=@mysql_connect("localhost","root","")?or?die(mysql_error());?
@mysql_select_db('demo',$conn)?or?die(mysql_error());?//?判断action?
$action?=?isset($_REQUEST['action'])?$_REQUEST['action']?:?'';?
//?上传图片?
if($action=='add'){?
$image?=?mysql_escape_string(file_get_contents($_FILES['photo']['tmp_name']));?
$type?=?$_FILES['photo']['type'];?
$sqlstr?=?"insert?into?photo(type,binarydata)?values('".$type."','".$image."')";?
@mysql_query($sqlstr)?or?die(mysql_error());?
header('location:upload_image_todb.php');?
exit();?
//?显示图片?
}elseif($action=='show'){?
$id?=?isset($_GET['id'])?intval($_GET['id'])?:?0;?
$sqlstr?=?"select?*?from?photo?where?id=$id";?
$query?=?mysql_query($sqlstr)?or?die(mysql_error());?
$thread?=?mysql_fetch_assoc($query);?
if($thread){?
header('content-type:'.$thread['type']);?
echo?$thread['binarydata'];?
}?
}else{?
//?显示图片列表及上传表单?
html?
head?
title?upload?image?to?db?demo?/title?
/head?
body?
form?name="form1"?method="post"?action="upload_image_todb.php"?enctype="multipart/form-data"?
p图片:input?type="file"?name="photo"/p?
pinput?type="hidden"?name="action"?value="add"input?type="submit"?name="b1"?value="提交"/p?
/form?
$sqlstr?=?"select?*?from?photo?order?by?id?desc";?
$result?=?array();?
while($thread=mysql_fetch_assoc($query)){?
$result[]?=?$thread;?
foreach($result?as?$val){?
echo?'pimg?
src="upload_image_todb.php?action=showid='.$val['id'].'t='.time().'"
/body?
/html?
程序运行截图和数据库截图:
以上就是土嘎嘎小编为大家整理的一个php图片上传的例子相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!