php文件图片批量上传程序代码

今天看到一个站长写了一个文件批量插件了,也是利用php写了,个人感觉还非常的不错于是整理一下给各位同学参考一下有兴趣的可下载测试一下.

说明:uploadx php批量上传组件遵循开源协议(GPL),任何个人、组织可自由对本程序进行使用、二次开发等权力.

由此也将声明本人不对您个人、组织使用本程序所带来的商业利益及损失有干涉及负责,但请保留版权信息,也欢迎对uploadx提出保贵的建议及意见,不胜感激.

本程序使用PHP程序编写,能更高效的批量处理PHP开发中的文件上传,图片处理、批量添加图片水印等问题,在使用本程序前请详细阅读使用说明.

HTML表单页,代码如下:

  1. <form enctype="multipart/form-data" action="uploadx.php" method="post">
  2. <input name="uploadx[]" type="file">
  3. <input name="uploadx[]" type="file">
  4. <input type="submit" value="上传">
  5. </form>

uploadx.php处理页:

  1. require ('./classes/uploadx.class.php');
  2. $uploadx = new uploadx;
  3. $uploadx->save = './temp';
  4. $uploadx->name = 'auto';
  5. $uploadx->mini = '200,200,mini';
  6. $uploadx->mark = './images/logo.png,0,60';
  7. 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 进行简单的介绍,也欢迎各位能在使用过程中进行改进补充.

例子代码如下:

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  4. <title>uploadx demo</title>
  5. </head>
  6. <body>
  7. <form enctype="multipart/form-data" action="uploadx.php" method="post">
  8. <input name="uploadx[]" type="file"> <br />
  9. <input name="uploadx[]" type="file"> <br />
  10. <input name="uploadx[]" type="file"> <br />
  11. <input name="uploadx[]" type="file"> <br />
  12. <br />
  13. <input type="submit" value="上传所选文件">
  14. </form>
  15. </body>
  16. </html>
  17. <?php
  18. require ('./classes/uploadx.class.php');
  19. $uploadx = new uploadx;
  20. $uploadx->save = './temp';
  21. $uploadx->name = 'auto';
  22. $uploadx->mini = '200,200,mini';
  23. $uploadx->mark = './images/logo.png,0,60';
  24. print_r($uploadx->mini('./temp/2.jpg'));

uploadx.class.php

  1. class uploadx {
  2. var $form = 'uploadx';
  3. var $save = './';
  4. var $size = '1024';
  5. var $type = 'gif,bmp,png,jpg,jpeg,swf,flv,mp3,wma,rar,zip,7z,doc,docx,ppt,pptx,xls,xlsx,txt,pdf';
  6. var $name = null;
  7. var $mini = null;
  8. var $mark = null;
  9. var $version = '2.0';
  10. //开源代码phpfensi.com
  11. public function is($type = true){
  12. foreach ($this->files() as $key => $val) {
  13. $file = $mini = null;
  14. $file = $this->saves($val['name'], $val['type'], $val['tmp_name'], $val['size'], $val['error']);
  15. $file['code'] || $file['path'] = rtrim($this->save,'/').'/'.$file['name'];
  16. $file['code'] || $file['mini'] = $this->mini($file['path']);
  17. $file['code'] || $file['mark'] = $this->mark($file['path']);
  18. $file['code'] && $file['error'] = $this->error($file['code']);
  19. $type ? $files[] = $file : ($file['code'] || $files[] = $file);
  20. }
  21. return isset($files) ? $files : array();
  22. }
  23. private function files(){
  24. if(count($_FILES[$this->form])<1) return array();
  25. if(is_array($_FILES[$this->form]['name'])){
  26. for($i=0; $i<count($_FILES[$this->form]['name']); $i++) {
  27. if($_FILES[$this->form]['error'][$i]==4) continue;
  28. $files[] = array(
  29. 'name'=>$_FILES[$this->form]['name'][$i],
  30. 'type'=>$_FILES[$this->form]['type'][$i],
  31. 'tmp_name'=>$_FILES[$this->form]['tmp_name'][$i],
  32. 'error'=>$_FILES[$this->form]['error'][$i],
  33. 'size'=>$_FILES[$this->form]['size'][$i]);
  34. }
  35. }else{
  36. $files[] = $_FILES[$this->form];
  37. }
  38. return $files;
  39. }
  40. private function saves($name, $type, $temp, $size, $error){
  41. if($error) return array('name'=>$name, 'code'=>$error);
  42. $prefix = strtolower(pathinfo($name, PATHINFO_EXTENSION));
  43. if(!in_array($prefix, explode(',', strtolower($this->type)))){
  44. return array('name'=>$name, 'code'=>'-1');
  45. }
  46. if($size/1024>$this->size){
  47. return array('name'=>$name, 'code'=>'-2');
  48. }
  49. if(!is_dir($this->save)){
  50. if(!mkdir($this->save, 0777, TRUE)){
  51. return array('name'=>$name, 'code'=>'-3');
  52. }
  53. }
  54. $filename = $this->name ? ($this->name=='auto' ? uniqid() : $this->name) : trim(basename($name,$prefix),'.');
  55. $savefile = trim($this->save,'/').'/'. $filename.'.'.$prefix;
  56. if(!@move_uploaded_file($temp,$savefile)){
  57. return array('name'=>$name, 'code'=>'-4');
  58. }
  59. @chmod($savefile,0777);
  60. return array('name'=>$filename.'.'.$prefix, 'code'=>0);
  61. }
  62. public function mini($file = null){
  63. if(!$file || !$this->mini) return false;
  64. if(!is_file($file)) return $this->error(-5,$file);
  65. list($width, $height, $extends) = explode(',', $this->mini);
  66. $types = array('gif','png','jpg','jpeg');
  67. $type = pathinfo($file, PATHINFO_EXTENSION);
  68. if(!in_array($type, $types)) return $this->error(-6);
  69. if(!is_file($file)) return $this->error(-5,$file);
  70. $mini = $extends ? basename($file, $type).$extends.'.'.$type : trim(basename($file),'.');
  71. $image = imagecreatefromstring(file_get_contents($file));
  72. $imagex = imagesx($image);
  73. $imagey = imagesy($image);
  74. $scale = $width / $imagex;
  75. if($width>$imagex){
  76. $mini_width = $imagex;
  77. $mini_height = $imagey;
  78. }else{
  79. $mini_width = $width;
  80. $mini_height = round($scale * $imagey);
  81. }
  82. if(function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled')){
  83. $temp = imagecreatetruecolor($mini_width, $mini_height);
  84. imagecopyresampled($temp,$image,0,0,0,0,$mini_width, $mini_height, $imagex, $imagey);
  85. }else{
  86. $temp = imagecreate($mini_width, $mini_height);
  87. imagecopyresized($temp,$image,0,0,0,0,$mini_width, $mini_height, $imagex, $imagey);
  88. }
  89. imagejpeg($temp, rtrim($this->save,'/').'/'.$mini, 100);
  90. imagedestroy($temp);
  91. imagedestroy($image);
  92. return is_file(rtrim($this->save,'/').'/'.$mini) ? $mini: false;
  93. }
  94. public function mark($file = null){
  95. if(!$file || !$this->mark) return false;
  96. list($watermark, $position, $opacity) = explode(',', $this->mark);
  97. if(!is_file($file) || !is_file($watermark)) return $this->error(-5,'FILE='.$file.'||'.'Watermark='.$watermark);
  98. $type = pathinfo($file, PATHINFO_EXTENSION);
  99. $types = array('gif','png','jpg','jpeg');
  100. if(!in_array($type, $types)) return $this->error(-6,$file);
  101. $opacity = min($opacity,100);
  102. $file_data = imagecreatefromstring(file_get_contents($file));
  103. $file_width = imagesx($file_data);
  104. $file_height = imagesy($file_data);
  105. if (in_array(pathinfo($watermark, PATHINFO_EXTENSION), array('gif','png'))) {
  106. $mark_data = imagecreatefromstring(file_get_contents($watermark));
  107. $mark_width = imagesx($mark_data);
  108. $mark_height = imagesy($mark_data);
  109. switch($position){
  110. case 1: $x = 5; $y = 5; break;
  111. case 2: $x = ($file_width - $mark_width)/2; $y = $mark_height; break;
  112. case 3: $x = ($file_width - $mark_width)-5; $y = $mark_height; break;
  113. case 4: $x = 5; $y = ($file_height - $mark_height) / 2; break;
  114. case 5: $x = ($file_width - $mark_width)/2; $y = ($file_height - $mark_height)/2; break;
  115. case 6: $x = ($file_width - $mark_width)-5; $y = ($file_height - $mark_height)/2; break;
  116. case 7: $x = 5; $y = ($file_height - $mark_height) - 5; break;
  117. case 8: $x = ($file_width - $mark_width)/2; $y = ($file_height - $mark_height)-5; break;
  118. case 9: $x = ($file_width - $mark_width)-5; $y = ($file_height - $mark_height)-5; break;
  119. default: $x = rand(0,($file_width - $mark_width)); $y = rand(0,($file_height - $mark_height));
  120. }
  121. $temp = imagecreatetruecolor($mark_width, $mark_height);
  122. imagecopy($temp, $file_data, 0, 0, $x, $y, $mark_width, $mark_height);
  123. imagecopy($temp, $mark_data, 0, 0, 0, 0, $mark_width, $mark_height);
  124. imagecopymerge($file_data, $temp, $x, $y, 0, 0, $mark_width, $mark_height, $opacity);
  125. imagejpeg($file_data, $file, 100);
  126. imagedestroy($temp);
  127. imagedestroy($file_data);
  128. imagedestroy($mark_data);
  129. return true;
  130. }else{
  131. return $this->error(-6,$watermark);
  132. } www.111cn.net
  133. }
  134. private function error($code = 0, $extends = ''){
  135. if($code){
  136. switch ($code) {
  137. case 6: $error = '写入临时文件夹失败'; break;
  138. case 5: $error = '写入系统临时文件夹错误'; break;
  139. case 4: $error = '没有文件被上传请检查表单'; break;
  140. case 3: $error = '文件上传出错上传不完整'; break;
  141. case 2: $error = '文件大小超出表单限制'; break;
  142. case 1: $error = '文件大小超出系统限制'; break;
  143. case -1: $error = '上传文件类型不合法'; break;
  144. case -2: $error = '上传文件大小超出后台限制'; break;
  145. case -3: $error = '创建文件保存路径失败'; break;
  146. case -4: $error = '保存文件失败请检查路径'; break;
  147. case -5: $error = '读取文件错误'; break;
  148. case -6: $error = '不支持该操作'; break;
  149. default: $error = '未知错误';
  150. }
  151. return '['.$code.']:'.$error.$extends;
  152. }else{
  153. return false;
  154. }
  155. }
  156. }