图片和传真查看器 PHP 图片上传代码

WBOY
Release: 2016-07-29 08:46:44
Original
1095 people have browsed it
PHP 图片上传代码
(代码片断试验成功,成功上传!)
因为昨天想起来学习一下PHP代码的冲动,是来源于像模仿着做一个类似公司IMAGE LIBRARY的东西出来。所以,今天当最基本的功能实现后,对PHP有了个概念性的认识后,就很自然地想到了个重要的功能急待实现,那就是图片的上传。
于是,开始在网上搜罗,找到个别人博客里的由三个页面组成的代码片断,于是拷下来研究,结果始终调试不成。本以为是公司电脑入域问题在作怪,后来经过一番尝试后,打算暂时放一下。回家去试(因为家里的电脑是最纯净的环境配制)。
到家后,将代码拷出来尝试了一下,依旧不行,同样的报错内容。很果断地,打算另辟蹊径,重新找个代码片断。哈,非常顺利,找到个仅一个页面的代码片断,一次尝试便成功了。CHEERS!
------------以下代码转载而来,贴上来和大家分享--------

复制代码代码如下:


$uptypes=array('image/jpg', //上传文件类型列表
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png');
$max_file_size=5000000; //上传文件大小限制, 单位BYTE
$destinati c repeat-x bottom}



上传文件:








允许上传的文件类型为:jpg|jpeg|png|pjpeg|gif|bmp|x-png|swf


返回

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
//是否存在文件
{
echo " 文件不存在!";
exit;
}
$file = $_FILES["upfile"];
if($max_file_size //检查文件大小
{
echo " 文件太大!";
exit;
}
if(!in_array($file["type"], $uptypes))
//检查文件类型
{
echo " 只能上传图像文件或Flash!";
exit;
}
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 " 同名文件已经存在了!";
exit;
}
if(!move_uploaded_file ($filename, $destination))
{
echo "移动文件出错!";
exit;
}
$pinfo=pathinfo($destination);
$fname=$pinfo[basename];
echo "已经成功上传
文件名:".$destination_folder.$fname."
";
echo " 宽度:".$image_size[0];
echo " 长度:".$image_size[1];
if($watermark==1)
{
$iinfo=getimagesize($destination,$iinfo);
$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
$white=imagecolorallocate($nimage,255,255,255);
$black=imagecolorallocate($nimage,0,0,0);
$red=imagecolorallocate($nimage,255,0,0);
imagefill($nimage,0,0,$white);
switch ($iinfo[2])
{
case 1:
$simage =imagecreatefromgif($destination);
break;
case 2:
$simage =imagecreatefromjpeg($destination);
break;
case 3:
$simage =imagecreatefrompng($destination);
break;
case 6:
$simage =imagecreatefromwbmp($destination);
break;
default:
die("不能上传此类型文件!");
exit;
}
imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);
switch($watertype)
{
case 1: //加水印字符串
imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
break;
case 2: //加水印图片
$simage1 =imagecreatefromgif("xplore.gif");
imagecopy($nimage,$simage1,0,0,0,0,85,15);
imagedestroy($simage1);
break;
}
switch ($iinfo[2])
{
case 1:
//imagegif($nimage, $destination);
imagejpeg($nimage, $destination);
break;
case 2:
imagejpeg($nimage, $destination);
break;
case 3:
imagepng($nimage, $destination);
break;
case 6:
imagewbmp($nimage, $destination);
//imagejpeg($nimage, $destination);
break;
}
//覆盖原上传文件
imagedestroy($nimage);
imagedestroy($simage);
}
if($imgpreview==1)
{
echo "
图片预览:
";
echo "\";
}
}
?>


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!