1,上传页面 i.php
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script type="text/javascript"> function startUpload() { // document.getElementById('processing').innerHTML = 'loding...'; return true; } function stopUpload(rel){ var rel = eval('(' + rel + ')'); document.getElementById('processing').innerHTML = document.getElementById('processing').innerHTML + '<img src="http://www.my.com/'+rel.thumbMap+'" alt="">'; } </script> </head> <body style="font-size:12px;"> <div id="processing"></div> <form action="upload.php" method="post" enctype="multipart/form-data" onsubmit="startUpload();" target="form-target" name="form1"> 本地图片:<input type="file" name="file"><br> 远程图片:<input type="text" name="url"><br> <input type="submit" name="Submit" value="提交"> <input name="scan" type="hidden" id="up" value="true"> </form> <iframe style="width:0; height:0; border:0;" name="form-target"></iframe> <b /><br/> </body> </html>
2. 上传类及操作上传文件 upload.php
<?php class ieb_upload { public $FormName; //文件域名称 public $Directroy; //上传至目录 public $MaxSize; //最大上传大小 public $CanUpload = true; //是否可以上传 public $doUpFile = ''; //上传的文件名 public $sm_File = ''; //缩略图名称 public $Error = 0; //错误参数 // 初始化(1024*2)*1024=2097152 就是 2M public function ieb_upload($FormName = '', $dirPath = '', $maxSize = 2097152) { // 初始化各种参数 $this->FormName = $FormName; $this->MaxSize = $maxSize; if ($this->FormName == '') { $this->CanUpload = false; $this->Error = 1; return; } $this->Directroy = $dirPath."/"; } //获取远程图片 public function GrabImage($url) { if($url=="") return false; $ext=strrchr($url,"."); if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") { return false; } //echo $filename=date("YmdHis").$ext; $fileName = $this->Directroy.$this->newName().$ext; //主图地址 $this->createDir(dirname($fileName)); ob_start(); readfile($url); $img = ob_get_contents(); ob_end_clean(); $size = strlen($img); $fp2 = @fopen($fileName, "a"); fwrite($fp2,$img); fclose($fp2); $this->doUpFile = $fileName; chmod($this->sm_File.$fileName, 0777); return true; } // 上传文件 public function upload() { if ($this->CanUpload) { if ($_FILES[$this->FormName]['size'] == 0) { $this->Error = 3; $this->CanUpload = false; return $this->Error; break; } $fileName =$this->Directroy.$this->newName().".".$this->getExt(); //主图地址 $this->createDir(dirname($fileName)); $doUpload = @copy($_FILES[$this->FormName]['tmp_name'], $fileName); if ($doUpload) { $this->doUpFile = $fileName; chmod($this->sm_File.$fileName, 0777); return true; } else { $this->Error = 4; return $this->Error; } } } // 创建图片缩略图 public function thumb($dscChar = '', $width = 150, $height = 150) { if ($this->CanUpload && $this->doUpFile != '') { if ($dscChar == '') $dscChar = 'sm_'; $data = getimagesize($this->doUpFile, $info); switch ($data[2]) { case 1: $this->sm_File = $this->doUpFile.$dscChar.".gif"; $im = @imagecreatefromgif($this->doUpFile); break; case 2: $this->sm_File = $this->doUpFile.$dscChar.".jpg"; $im = @imagecreatefromjpeg($this->doUpFile); break; case 3: $this->sm_File = $this->doUpFile.$dscChar.".png"; $im = @imagecreatefrompng($this->doUpFile); break; } $srcW = imagesx($im); $srcH = imagesy($im); $ni = imagecreatetruecolor($width, $height); imagecopyresized($ni, $im, 0, 0, 0, 0, $width, $height, $srcW, $srcH); $cr = imagejpeg($ni, $this->sm_File); chmod($this->sm_File, 0777); if ($cr) { return true; } else { $this->Error = 5; return $this->Error; } } } // 检查文件是否存在 public function scanFile() { if ($this->CanUpload) { $scan = is_readable($_FILES[$this->FormName]['name']); if ($scan) $this->Error = 2; return $scan; } } // 获取文件大小 public function getSize($format = 'B') { if ($this->CanUpload) { if ($_FILES[$this->FormName]['size'] == 0) { $this->Error = 3; $this->CanUpload = false; } switch ($format) { case 'B':return $_FILES[$this->FormName]['size'];break; case 'K':return ($_FILES[$this->FormName]['size']) / (1024);break; case 'M':return ($_FILES[$this->FormName]['size']) / (1024 * 1024);break; } } } // 获取文件类型 public function getExt() { if ($this->CanUpload){ $ext = $_FILES[$this->FormName]['name']; $extStr = explode('.', $ext); $count = count($extStr)-1; return $extStr[$count]; } } // 获取文件名称 public function getName() { if ($this->CanUpload) return $_FILES[$this->FormName]['name']; } //创建文件夹 public function createDir($path){ if (!file_exists($path)){ $this->createDir(dirname($path)); mkdir($path, 0777); } } // 新建文件名 public function newName() { if ($this->CanUpload) { //$FullName = $_FILES[$this->FormName]['name']; //$extStr = explode('.', $FullName); //$count = count($extStr)-1; //$ext = $extStr[$count]; //return date('YmdHis').rand(0, 9).'.'.$ext; return date('YmdHis').rand(0, 9); } } // 显示错误参数 public function Err() { return $this->Error; } // 上传后的文件名 public function UpFile() { if ($this->doUpFile != '') return $this->doUpFile; else $this->Error = 16; } // 上传文件的路径 public function filePath() { if ($this->doUpFile != '') return $this->sm_File.$this->doUpFile; else $this->Error = 26; } // 缩略图文件名称 public function thumbMap() { if ($this->sm_File != '') return $this->sm_File; else $this->Error = 36; } // 显示版本信息 public function ieb_version() { return 'IEB_UPLOAD CLASS Ver 1.1'; } } if (isset($_REQUEST['scan'])) { // 声明一个上传类 $upfos = new ieb_upload('file','open1/33'); //远程图片 if($_REQUEST['url']){ $upfos->GrabImage($_REQUEST['url']); }else{ $upfos->upload();//上传主图 } $upfos->thumb(); //生成缩略图 //返回数组 $re['name'] = $upfos->getName();//文件名 $re['ext'] = $upfos->getExt(); //扩展名 $re['size'] = $upfos->getSize(); //扩展名 $re['newName'] = $upfos->UpFile(); //新文件名 $re['filePath'] = $upfos->filePath(); //文件路径 $re['thumbMap'] = $upfos->thumbMap(); //文件路径 } ?> <script type="text/javascript"> var re = '<?php echo json_encode($re); ?>'; window.top.window.stopUpload(re); </script>
需要用的,自己可以拿去封装
效果: