123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- class FileUpload
- {
- static public function recursive_makedir($path)
- {
- return is_dir($path) or (self::recursive_makedir(dirname($path)) and @mkdir($path));
- }
- static private function clean_up($file, $result)
- {
- @unlink($file);
- return $result;
- }
- static public function upload($img_savedir, $is_img = true, $overwrite = false)
- {
- $tmppath = $_POST["file_path"];
- $sha1 = $_POST["file_sha1"];
- $file_name = $_POST["file_name"];
- $result = array();
- $result["ret"] = false;
- $result["file"] = "";
- if (!isset($tmppath) || empty($tmppath)) {
- return self::clean_up($tmppath, $result);
- }
- if (!isset($sha1) || empty($sha1)) {
- return self::clean_up($tmppath, $result);
- }
- if (!isset($file_name) || empty($file_name)) {
- return self::clean_up($tmppath, $result);
- }
- if ($is_img) {
- $ext_name = strtolower(pathinfo($file_name)['extension']);
- if (!($ext_name == 'jpg' || $ext_name == 'jpeg' || $ext_name == 'png' || $ext_name == 'gif' || $ext_name == 'bmp')) {
- return self::clean_up($tmppath, $result);
- }
- }
- if (!isset($img_savedir)) {
- return self::clean_up($tmppath, $result);
- }
- $timestamp = date("YmdHis", time());
- $dest_file = $img_savedir . "/" . substr($timestamp, 0, 4) . "/" . substr($sha1, 0, 2) . "/" . $sha1 . '.' . $ext_name;
- if (!self::recursive_makedir(dirname($dest_file))) {
- return self::clean_up($tmppath, $result);
- }
- if (!$overwrite) {
- if (!file_exists($dest_file)) {
- if (!copy($tmppath, $dest_file)) {
- return self::clean_up($tmppath, $result);
- }
- }
- } else {
- if (!copy($tmppath, $dest_file)) {
- return self::clean_up($tmppath, $result);
- }
- }
- $result["ret"] = true;
- $result["file"] = $dest_file;
- return self::clean_up($tmppath, $result);
- }
- }
- ?>
|