1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- class FileUpload
- {
- const upload_store_path = BASE_ROOT_PATH . '/data/upload/uploadtmp';
- static private function recursive_makedir($path)
- {
- return is_dir($path) or (self::recursive_makedir(dirname($path)) and @mkdir($path));
- }
- static private function get_random_string($length) {
- $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
- $max = strlen($strPol)-1;
- $str = '';
- for($i=0; $i < $length; $i++) {
- $str.=$strPol[rand(0,$max)];
- }
- return $str;
- }
- static private function upload_normal($src_file,$dest_dir,&$file_path)
- {
- if(!file_exists($src_file)) {
- return false;
- }
- $file_name = strtolower(pathinfo($src_file)['basename']);
- return self::do_upload($src_file,$file_name,$dest_dir,$file_path);
- }
- static public function upload_avatar(&$file_path)
- {
- return self::upload_nginx(BASE_AVATAR_PATH,$_SESSION['member_id'],$file_path);
- }
- static private function upload_nginx($dest_dir,$member_id,&$file_path)
- {
- $tmppath = $_POST["file_path"];
- $file_name = $_POST["src_name"];
- return self::do_upload($tmppath,$file_name,$dest_dir,$member_id,$file_path);
- }
- static private function do_upload($src_path,$file_name,$dest_dir,$member_id,&$file_path)
- {
- if (!isset($src_path) || empty($src_path)) {
- return false;
- }
- $src_path = self::upload_store_path . $src_path;
- if(!file_exists($src_path)) {
- return false;
- }
- if (!isset($dest_dir) || empty($dest_dir)) {
- @unlink($src_path);
- return false;
- }
- if (!isset($member_id) || empty($member_id)) {
- @unlink($src_path);
- return false;
- }
- $ext_name = strtolower(pathinfo($file_name)['extension']);
- $dest_fn = strval($member_id) . self::get_random_string(10) . '.' . $ext_name;
- $dest_file = $dest_dir . "/" . sprintf("%03d", intval(intval($member_id) / 2000)) . "/" . $dest_fn;
- if (!self::recursive_makedir(dirname($dest_file))) {
- @unlink($src_path);
- return false;
- }
- if (!copy($src_path, $dest_file)) {
- @unlink($src_path);
- return false;
- } else {
- $file_path = $dest_file;
- @unlink($src_path);
- return true;
- }
- }
- }
- ?>
|