今天看到一個站長寫了一個文件批量插件了,也是利用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
- 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';
- //開源代碼Vevb.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;
- }
- }
- }
新聞熱點
疑難解答