
<php
/
参数说明:
$max_file_size : 上传文件大小限制, 单位BYTE
$destination_folder : 上传文件路径
$watermark : 是否附加水印(1为加水印,其他为不加水印);
/
//上传文件类型列表
$uptypes=array(
'image/jpg',
'image/jpeg',
'image/png',
'image/gif'
);
$max_file_size = 2000000; //上传文件大小限制, 单位BYTE
$des_folder = "/img/"; //上传文件路径
>
<html>
<head>
<title>上传程序</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" name="upform">
上传文件:
<input name="upfile" type="file">
<input type="submit" value="上传"><br>
允许上传的文件类型为:<=implode(', ',$uptypes)>
</form>
<php
if ($_SERVER['REQUEST_METHOD'] != 'POST') exit;
if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
//是否存在文件
{
echo "不存在!";
exit;
}
$file = $_FILES["upfile"];
if($max_file_size < $file["size"])
//检查文件大小
{
echo "文件太大!";
exit;
}
if(!in_array($file["type"], $uptypes))
//检查文件类型
{
echo "文件类型不符!"$file["type"];
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_foldertime()""$ftype;
if (file_exists($destination) && $overwrite != true)
{
echo "同名文件已经存在了";
exit;
}
if(!move_uploaded_file ($filename, $destination))
{
echo "移动文件出错";
exit;
}
$pinfo=pathinfo($destination);
$fname=$pinfo[basename];
echo " <font color=red>已经成功上传</font><br>
文件名: <font color=blue>"$destination_folder$fname"</font><br>";
echo " 宽度:"$image_size[0];
echo " 长度:"$image_size[1];
echo "<br> 大小:"$file["size"]" bytes";
>
<php
/
Programmer : Msn/QQ haowubai@hotmailcom (925939)
>
blob类型
<php
/
文件名:upload_filephp
Copyright @ 2009
创建人:tabor
日期:2009年7月24日 8:00
修改人:
日期:
描述:文件上传 *** 作以及对文件的处理
版本:
/
class upload_file {
//保存的文件名
public $file_name;
//系统中上传文件的临时存放路径
public $file_tmp_name;
//文件大小
public $file_size;
//完整的文件类型
public $full_file_type;
//文件类型
public $file_type;
//文件是否覆盖
public $override = 1;
//文件的保存路径
public $file_save_path = '';
//上传文件大小的最大值 单位是字节 2M
public public $file_max_size = 210000000;
//public public $file_max_size = 102400;
//构造函数
function __construct($file_name = '', $file_tmp_name = '', $full_file_type = '', $file_size = '', $file_save_path = '') {
$this->file_name = $file_name;
$this->file_tmp_name = $file_tmp_name;
$this->full_file_type = $full_file_type;
$this->file_size = $file_size;
$this->file_save_path = $file_save_path;
}
//取得文件的后缀名,即文件类型
function get_file_type() {
$type_array = explode('', $this->file_name);
return $type_array[count($type_array)-1];
}
//判断文件的大小
function check_size() {
if($this->file_size > $this->file_max_size) {
return false;
}
return true;
}
//取得文件的大小
function get_size() {
return intval($this->file_size/1024);
}
//上传 格式 jpg,png,gif,pjpeg
function check_upload_pic() {
$type = $this->get_file_type();
$type_array = array('jpg', 'png', 'gif', 'bmp');
foreach($type_array as $value) {
if($value = $type) {
return true;
}
return false;
}
}
//上传文件 格式 zip rar
function check_upload_file() {
$type = $this->get_file_type();
$type_array = array('jpg','gif','bmp','png');
foreach($type_array as $value) {
if($value == $type) {
return true;
}
return false;
}
}
//判断文件是否存在
function check_exist() {
$file = $this->file_save_path$this->file_name;
return file_exists($file);
}
//上传文件
function move_upfile() {
if(!$this->check_upload_pic()) {
echo "ok1";
return false;
}
else {
if(!$this->check_size()) {
echo "ok2";
return false;
}
else {
// if($this->check_exist()) {
// echo "该文件已存在";
// return false;
// }
// else {
$path = $this->file_save_path$this->file_name;
if(move_uploaded_file($this->file_tmp_name, $path)) {
return true;
}
else {
return false;
}
// }
}
}
}
//将上传的打水印
/
$water_pic_name 将要被打水印的目标
$water_word 水印文字
$path 将来生成水印的存放路径
/
function create_water_pic($water_word) {
$type = $this->get_file_type();
$filename = $this->file_save_path$this->file_name;
switch($type) {
case 'jpg':
header("content-type:image/jpeg"); //定义输出图像的类型
$im = imagecreatefromjpeg($filename); //载入
break;
case 'png':
header("content-type:image/png");
$im = imagecreatefrompng($filename);
break;
case 'gif':
header("content-type:image/gif");
$im = imagecreatefromgif($filename);
break;
case 'bmp':
header("content-type:image/xbm"); //上传bmp格式存在问题
$im = imagecreatefromxbm($filename); //无法打水印
break;
default: {
echo "文件格式不符";
}
}
$textcolor = imagecolorallocate($im, 56, 73,136); //设定字体的颜色
$font = "simheittf"; //定义字体
$word = $water_word; //水印字符
$x = imagesx($im); //获取的宽度
$y = imagesy($im); //获取文件的高度
$position_x = $x-80;
$position_y = $y-10;
$str = iconv('gbk', 'utf-8', $word); //将中文文字显示出来的编码过程
imagettftext($im, 20, 0, $position_x, $position_y, $textcolor, $font, $str);
//imagejpeg($im); //显示
$new = $this->file_save_path'water'$this->file_name; //生成新的文件名
switch($type) {
case 'jpg':
imagejpeg($im, $new); //生成jpg图像
break;
case 'png':
imagepng($im, $new); //生成png图像
break;
case 'gif':
imagegif($im, $new); //生成gif图像
break;
case 'bmp':
imagexbm($im, $new); //生成bmp图像 该格式的文件处理有问题
break;
default: {
echo "文件格式不符";
}
}
imagedestroy($im); //结束图形,释放内存空间/
}
//生成缩略图
/
$pic 名 包括其扩展名,但不包括路径
$width 将来生成缩略图的宽度
$height 将来生成缩略图的高度
$path 生成缩略图的存放路径
/
function create_thumbnail($width, $height) {
$type = $this->get_file_type();
$filename = $this->file_save_path$this->file_name;
$img = getimagesize($filename);
//print_r($img);
//die();
switch($img[2]) {
case 1:
header("content-type:image/gif"); //定义输出图像的类型
$im = imagecreatefromgif($filename); //载入
break;
case 2:
header("content-type:image/jpeg");
$im = imagecreatefromjpeg($filename);
break;
case 3:
header("content-type:image/png");
$im = imagecreatefrompng($filename);
break;
case 6:
header("content-type:image/xbm"); //bmp格式存在问题
$im = imagecreatefromxbm($filename); //无法打水印
break;
default: {
echo "文件格式不符";
}
}
$thumb = imagecreatetruecolor($width, $height); //创建一个新的空白的面板
$color = imagecolorallocate($im, 200, 255, 100); //调色板
/bool imagecopyresized ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )
imagecopyresized() 将一幅图像中的一块正方形区域拷贝到另一个图像中。dst_image 和 src_image 分别是目标图像和源图像的标识符。
/
imagecopyresized($thumb, $im, 0, 0, 0, 0, $width, $height, $img[0], $img[1]);
//imagejpeg($thumb);
$thumb_path = $this->file_save_path"thumbnail/"$this->file_name;
switch($img[2]) {
case 1:
imagejpeg($thumb, $thumb_path);
break;
case 2:
imagegif($thumb, $thumb_path);
break;
case 3:
imagepng($thumb, $thumb_path);
break;
case 6:
imagexbm($thumb, $thumb_path);
break;
default: {
echo "文件格式不符";
}
}
}
}
>
前几天做的一个类,可以正常的使用,但还存在问题,仅供参考!忘对您有所帮助
以上就是关于php 实现图片上传如何实现全部的内容,包括:php 实现图片上传如何实现、用php处理后的图片怎样上传至指定目录或上传到数据库、php把图片上传到数据库并显示等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)