php文件图片批量上传程序代码
今天看到一个站长写了一个文件批量插件了,也是利用php写了,个人感觉还非常的不错于是整理一下给各位同学参考一下有兴趣的可下载测试一下.
说明:uploadx php批量上传组件遵循开源协议(GPL),任何个人、组织可自由对本程序进行使用、二次开发等权力.
由此也将声明本人不对您个人、组织使用本程序所带来的商业利益及损失有干涉及负责,但请保留版权信息,也欢迎对uploadx提出保贵的建议及意见,不胜感激.
本程序使用PHP程序编写,能更高效的批量处理PHP开发中的文件上传,图片处理、批量添加图片水印等问题,在使用本程序前请详细阅读使用说明.
HTML表单页,代码如下:
<form enctype="multipart/form-data" action="uploadx.php" method="post">
<input name="uploadx[]" type="file">
<input name="uploadx[]" type="file">
<input type="submit" value="上传">
</form>
uploadx.php处理页:
require ('./classes/uploadx.class.php');
$uploadx = new uploadx;
$uploadx->save = './temp';
$uploadx->name = 'auto';
$uploadx->mini = '200,200,mini';
$uploadx->mark = './images/logo.png,0,60';
print_r($uploadx->is());
参数说明:
$uploadx->save
上传文件的保存目录
$uploadx->name;
上传文件的命名方式
参数说明
auto=自动随机文件名
null=原文件名(覆盖)
其他自定义如按时间:date('YmdHis');
$uploadx->mini;
生成缩略图参数
width = 生成缩略图宽度。
height = 生成缩略图高度。
mini = 生成缩略图文件后缀附加名
默认留空不生成缩略图;
$uploadx->mark;
上传图片文件添加水印参数:
水印文件,水印位置,水印透明度
位置参数说明:
0 = 随机;1 = 左上角;2 = 顶部居中;3 = 右上角;4 = 左居中;5 = 中部居中;6 = 右居中;7 = 左下角;8 = 底部居中;9 = 右下角;
$uploadx->is(true);返回已上传文件的数组
is参数说明:
true = 返回所有上传文件
false = 过滤失败文件
$uploadx->is();
返回数据数组格式说明:
name = 上传后已保存的文件名
mini = 生成缩略图的文件名
mark = 添加水印状态,1表示成功,否则失败
code = 错误代码:0表示上传成功
error = 错误信息:上传错误时提示的错误信息
其中生成缩略图和添加水印功能也可单独使用,以上是对uploadx2.0 进行简单的介绍,也欢迎各位能在使用过程中进行改进补充.
例子代码如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>uploadx demo</title>
</head>
<body>
<form enctype="multipart/form-data" action="uploadx.php" method="post">
<input name="uploadx[]" type="file"> <br />
<input name="uploadx[]" type="file"> <br />
<input name="uploadx[]" type="file"> <br />
<input name="uploadx[]" type="file"> <br />
<br />
<input type="submit" value="上传所选文件">
</form>
</body>
</html>
<?php
require ('./classes/uploadx.class.php');
$uploadx = new uploadx;
$uploadx->save = './temp';
$uploadx->name = 'auto';
$uploadx->mini = '200,200,mini';
$uploadx->mark = './images/logo.png,0,60';
print_r($uploadx->mini('./temp/2.jpg'));
?>
uploadx.class.php
<?php
class uploadx {
var $form = 'uploadx';
var $save = './';
var $size = '1024';
var $type = 'gif,bmp,png,jpg,jpeg,swf,flv,mp3,wma,rar,zip,7z,doc,docx,ppt,pptx,xls,xlsx,txt,pdf';
var $name = null;
var $mini = null;
var $mark = null;
var $version = '2.0';
//开源代码phprm.com
public function is($type = true) {
foreach ($this->files() as $key => $val) {
$file = $mini = null;
$file = $this->saves($val['name'], $val['type'], $val['tmp_name'], $val['size'], $val['error']);
$file['code'] || $file['path'] = rtrim($this->save, '/') . '/' . $file['name'];
$file['code'] || $file['mini'] = $this->mini($file['path']);
$file['code'] || $file['mark'] = $this->mark($file['path']);
$file['code'] && $file['error'] = $this->error($file['code']);
$type ? $files[] = $file : ($file['code'] || $files[] = $file);
}
return isset($files) ? $files : array();
}
private function files() {
if (count($_FILES[$this->form]) < 1) return array();
if (is_array($_FILES[$this->form]['name'])) {
for ($i = 0; $i < count($_FILES[$this->form]['name']); $i++) {
if ($_FILES[$this->form]['error'][$i] == 4) continue;
$files[] = array(
'name' => $_FILES[$this->form]['name'][$i],
'type' => $_FILES[$this->form]['type'][$i],
'tmp_name' => $_FILES[$this->form]['tmp_name'][$i],
'error' => $_FILES[$this->form]['error'][$i],
'size' => $_FILES[$this->form]['size'][$i]
);
}
} else {
$files[] = $_FILES[$this->form];
}
return $files;
}
private function saves($name, $type, $temp, $size, $error) {
if ($error) return array(
'name' => $name,
'code' => $error
);
$prefix = strtolower(pathinfo($name, PATHINFO_EXTENSION));
if (!in_array($prefix, explode(',', strtolower($this->type)))) {
return array(
'name' => $name,
'code' => '-1'
);
}
if ($size / 1024 > $this->size) {
return array(
'name' => $name,
'code' => '-2'
);
}
if (!is_dir($this->save)) {
if (!mkdir($this->save, 0777, TRUE)) {
return array(
'name' => $name,
'code' => '-3'
);
}
}
$filename = $this->name ? ($this->name == 'auto' ? uniqid() : $this->name) : trim(basename($name, $prefix) , '.');
$savefile = trim($this->save, '/') . '/' . $filename . '.' . $prefix;
if (!@move_uploaded_file($temp, $savefile)) {
return array(
'name' => $name,
'code' => '-4'
);
}
@chmod($savefile, 0777);
return array(
'name' => $filename . '.' . $prefix,
'code' => 0
);
}
public function mini($file = null) {
if (!$file || !$this->mini) return false;
if (!is_file($file)) return $this->error(-5, $file);
list($width, $height, $extends) = explode(',', $this->mini);
$types = array(
'gif',
'png',
'jpg',
'jpeg'
);
$type = pathinfo($file, PATHINFO_EXTENSION);
if (!in_array($type, $types)) return $this->error(-6);
if (!is_file($file)) return $this->error(-5, $file);
$mini = $extends ? basename($file, $type) . $extends . '.' . $type : trim(basename($file) , '.');
$image = imagecreatefromstring(file_get_contents($file));
$imagex = imagesx($image);
$imagey = imagesy($image);
$scale = $width / $imagex;
if ($width > $imagex) {
$mini_width = $imagex;
$mini_height = $imagey;
} else {
$mini_width = $width;
$mini_height = round($scale * $imagey);
}
if (function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled')) {
$temp = imagecreatetruecolor($mini_width, $mini_height);
imagecopyresampled($temp, $image, 0, 0, 0, 0, $mini_width, $mini_height, $imagex, $imagey);
} else {
$temp = imagecreate($mini_width, $mini_height);
imagecopyresized($temp, $image, 0, 0, 0, 0, $mini_width, $mini_height, $imagex, $imagey);
}
imagejpeg($temp, rtrim($this->save, '/') . '/' . $mini, 100);
imagedestroy($temp);
imagedestroy($image);
return is_file(rtrim($this->save, '/') . '/' . $mini) ? $mini : false;
}
public function mark($file = null) {
if (!$file || !$this->mark) return false;
list($watermark, $position, $opacity) = explode(',', $this->mark);
if (!is_file($file) || !is_file($watermark)) return $this->error(-5, 'FILE=' . $file . '||' . 'Watermark=' . $watermark);
$type = pathinfo($file, PATHINFO_EXTENSION);
$types = array(
'gif',
'png',
'jpg',
'jpeg'
);
if (!in_array($type, $types)) return $this->error(-6, $file);
$opacity = min($opacity, 100);
$file_data = imagecreatefromstring(file_get_contents($file));
$file_width = imagesx($file_data);
$file_height = imagesy($file_data);
if (in_array(pathinfo($watermark, PATHINFO_EXTENSION) , array(
'gif',
'png'
))) {
$mark_data = imagecreatefromstring(file_get_contents($watermark));
$mark_width = imagesx($mark_data);
$mark_height = imagesy($mark_data);
switch ($position) {
case 1:
$x = 5;
$y = 5;
break;
case 2:
$x = ($file_width - $mark_width) / 2;
$y = $mark_height;
break;
case 3:
$x = ($file_width - $mark_width) - 5;
$y = $mark_height;
break;
case 4:
$x = 5;
$y = ($file_height - $mark_height) / 2;
break;
case 5:
$x = ($file_width - $mark_width) / 2;
$y = ($file_height - $mark_height) / 2;
break;
case 6:
$x = ($file_width - $mark_width) - 5;
$y = ($file_height - $mark_height) / 2;
break;
case 7:
$x = 5;
$y = ($file_height - $mark_height) - 5;
break;
case 8:
$x = ($file_width - $mark_width) / 2;
$y = ($file_height - $mark_height) - 5;
break;
case 9:
$x = ($file_width - $mark_width) - 5;
$y = ($file_height - $mark_height) - 5;
break;
default:
$x = rand(0, ($file_width - $mark_width));
$y = rand(0, ($file_height - $mark_height));
}
$temp = imagecreatetruecolor($mark_width, $mark_height);
imagecopy($temp, $file_data, 0, 0, $x, $y, $mark_width, $mark_height);
imagecopy($temp, $mark_data, 0, 0, 0, 0, $mark_width, $mark_height);
imagecopymerge($file_data, $temp, $x, $y, 0, 0, $mark_width, $mark_height, $opacity);
imagejpeg($file_data, $file, 100);
imagedestroy($temp);
imagedestroy($file_data);
imagedestroy($mark_data);
return true;
} else {
return $this->error(-6, $watermark);
}
www.111cn . net
}
private function error($code = 0, $extends = '') {
if ($code) {
switch ($code) {
case 6:
$error = '写入临时文件夹失败';
break;
case 5:
$error = '写入系统临时文件夹错误';
break;
case 4:
$error = '没有文件被上传请检查表单';
break;
case 3:
$error = '文件上传出错上传不完整';
break;
case 2:
$error = '文件大小超出表单限制';
break;
case 1:
$error = '文件大小超出系统限制';
break;
case -1:
$error = '上传文件类型不合法';
break;
case -2:
$error = '上传文件大小超出后台限制';
break;
case -3:
$error = '创建文件保存路径失败';
break;
case -4:
$error = '保存文件失败请检查路径';
break;
case -5:
$error = '读取文件错误';
break;
case -6:
$error = '不支持该操作';
break;
default:
$error = '未知错误';
}
return '[' . $code . ']:' . $error . $extends;
} else {
return false;
}
}
}
?>
本文地址:http://www.phprm.com/scxz/fs4823.html
转载随意,但请附上文章地址:-)