~~~ <?php namespace jqstu; class Jqfile { function file_one_img_upload($name,$save_path='',$allow_type=array(),$big_size=2000000){ $file=$_FILES[$name]; if ($file['error']> 0){ $errormsg= self::file_upload_error($file['error']); Jqutil::JqException("{$errormsg}"); } // 设置文件上传限制 if ($file['size']>=$big_size){ Jqutil::JqException("filesize not match"); } $file['save_size']=self::file_sizecount($file['size']); //文件后缀 $file['ext']=strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); //检查文件类型 $allow_type=array_merge($allow_type,array('jpg','jpeg','png','pjpeg','gif')); if (!in_array($file['ext'] ,$allow_type)){ Jqutil::JqException("mimetype to upload is not allowed"); } //文件存放的目录 if (empty($save_path)){ $file['save_path'] = "img" .DS. date('Y/m/'); }else{ $file['save_path'] = self::parse_path($save_path); } if ( !self::mkdirs( $file['save_path'])) { Jqutil::JqException("{$file['save_path']} Failed to create ,Insufficient permissions "); } //文件名字 $file['save_name']=self::file_name_build().".".$file['ext']; //保存文件完整地址 $file['save_path_name']=$file['save_path'].$file['save_name']; // if (is_file($file['save_path_name'])){ // Jqutil::JqException("has the same filename: {$file['save_path_name']}"); // }else{ // move_uploaded_file($file['tmp_name'],$file['save_path_name']); // } if (is_file($file['save_path_name'])){ unlink($file['save_path_name']); } if (!file_put_contents($file['save_path_name'],file_get_contents($file['tmp_name'], FILE_APPEND))){ $file['status']=502; } else{ if (filesize($file['save_path_name'])==$file['size']){ $file['status']=200; }else{ $file['status']=502; } } return $file; } /** * @return string */ public function file_name_build(){ return $savename = date('Ymd').md5(microtime(true)); } /** * @param $imgUrl * @param string $limit * @param string $path * @return string * @throws \Exception */ function file_remote_attach_fetch($imgUrl , $limit = '5000', $path = ''){ $http=new JqHttp(); $resp = $http->ihttp_get($imgUrl); //获取请求头并检测死链 if (intval($resp['code']) != 200){ Jqutil::JqException("提取文件失败: 未找到该资源文件."); } $ext = $type = ''; switch ($resp['headers']['Content-Type']) { case 'application/x-jpg': case 'image/jpg': case 'image/jpeg': $ext = 'jpg'; $type = 'images'; break; case 'image/png': $ext = 'png'; $type = 'images'; break; case 'image/gif': $ext = 'gif'; $type = 'images'; break; case 'video/mp4': case 'video/mpeg4': $ext = 'mp4'; $type = 'videos'; break; case 'video/x-ms-wmv': $ext = 'wmv'; $type = 'videos'; break; case 'audio/mpeg': $ext = 'mp3'; $type = 'audios'; break; case 'audio/mp4': $ext = 'mp4'; $type = 'audios'; break; case 'audio/x-ms-wma': $ext = 'wma'; $type = 'audios'; break; default: Jqutil::JqException("提取资源失败, 资源文件类型错误"); break; } if (empty($path)){ $path = $type . "/" . date('Y/m/'); }else{ $path = self::parse_path($path); } if ( !self::mkdirs( $path)) { Jqutil::JqException("提取文件失败: 权限不足."); } $limit = $limit * 1024; if (intval($resp['headers']['Content-Length']) > $limit) { Jqutil::JqException('上传的媒体文件过大(' . self::file_sizecount($resp['headers']['Content-Length']) . ' > ' . self::sizecount($limit)); } $filename = self::file_random_name( $path, $ext); $pathname = $path . $filename; $fullname = $pathname; if (file_put_contents($fullname, $resp['content']) == false) { Jqutil::JqException("提取失败"); } return $pathname; } /** * @param $dir * @param $ext * @return string */ function file_random_name($dir, $ext) { do { $filename = date('Ymdhis').mt_rand(100,999) . '.' . $ext; } while (file_exists($dir . $filename)); return $filename; } /** * @param $size * @return string */ function file_sizecount($size) { if($size >= 1073741824) { $size = round($size / 1073741824 * 100) / 100 . ' GB'; } elseif($size >= 1048576) { $size = round($size / 1048576 * 100) / 100 . ' MB'; } elseif($size >= 1024) { $size = round($size / 1024 * 100) / 100 . ' KB'; } else { $size = $size . ' Bytes'; } return $size; } /** * @param $errorNo * @return string */ private function file_upload_error($errorNo){ switch ($errorNo) { case 1: case 2: $error = 'upload File size exceeds the maximum value'; break; case 3: $error = 'only the portion of file is uploaded'; break; case 4: $error = 'no file to uploaded'; break; case 6: $error = 'upload temp dir not found'; break; case 7: $error = 'file write error'; break; default: $error = 'unknown upload error'; } return $error; } /** * @param $path * @return bool */ function mkdirs($path) { if (is_dir($path)) { return true; } if (mkdir($path, 0755, true)) { return true; } else { return false; } } /** * @param $path * @return bool */ function parse_path($path) { $danger_char = array('../', '{php', '<?php', '<%', '<?', '..\\', '\\\\' ,'\\', '..\\\\', '%00', '\0', '\r'); foreach ($danger_char as $char) { if (Jqutil::strexists($path, $char)) { return false; } } return $path; } } ~~~