|
@@ -7,12 +7,13 @@ class FileUpload
|
|
|
return is_dir($path) or (self::recursive_makedir(dirname($path)) and @mkdir($path));
|
|
|
}
|
|
|
|
|
|
- static private function unlink_tmpfiles($file)
|
|
|
+ static private function clean_up($file, $result)
|
|
|
{
|
|
|
@unlink($file);
|
|
|
+ return $result;
|
|
|
}
|
|
|
|
|
|
- static public function upload($img_savedir, $subdir, $is_img = true, $overwrite = false)
|
|
|
+ static public function upload($img_savedir, $is_img = true, $overwrite = false)
|
|
|
{
|
|
|
$tmppath = $_POST["file_path"];
|
|
|
$sha1 = $_POST["file_sha1"];
|
|
@@ -23,55 +24,51 @@ class FileUpload
|
|
|
$result["file"] = "";
|
|
|
|
|
|
if (!isset($tmppath) || empty($tmppath)) {
|
|
|
- self::unlink_tmpfiles($tmppath);
|
|
|
- return $result;
|
|
|
+ return self::clean_up($tmppath, $result);
|
|
|
}
|
|
|
|
|
|
if (!isset($sha1) || empty($sha1)) {
|
|
|
- self::unlink_tmpfiles($tmppath);
|
|
|
- return $result;
|
|
|
+ return self::clean_up($tmppath, $result);
|
|
|
}
|
|
|
|
|
|
if (!isset($file_name) || empty($file_name)) {
|
|
|
- self::unlink_tmpfiles($tmppath);
|
|
|
- return $result;
|
|
|
+ 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')) {
|
|
|
- self::unlink_tmpfiles($tmppath);
|
|
|
- return $result;
|
|
|
+ return self::clean_up($tmppath, $result);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (!isset($img_savedir) || !isset($subdir)) {
|
|
|
- self::unlink_tmpfiles($tmppath);
|
|
|
- return $result;
|
|
|
+ if (!isset($img_savedir)) {
|
|
|
+ return self::clean_up($tmppath, $result);
|
|
|
}
|
|
|
|
|
|
$timestamp = date("YmdHis", time());
|
|
|
- $dest_file = $img_savedir . "/" . $subdir . "/" . substr($timestamp, 0, 4) . "/" . substr($sha1, 0, 2) . "/" . $sha1 . '.' . $ext_name;
|
|
|
+ $dest_file = $img_savedir . "/" . substr($timestamp, 0, 4) . "/" . substr($sha1, 0, 2) . "/" . $sha1 . '.' . $ext_name;
|
|
|
|
|
|
if (!self::recursive_makedir(dirname($dest_file))) {
|
|
|
- self::unlink_tmpfiles($tmppath);
|
|
|
- return $result;
|
|
|
+ return self::clean_up($tmppath, $result);
|
|
|
}
|
|
|
|
|
|
if (!$overwrite) {
|
|
|
if (!file_exists($dest_file)) {
|
|
|
- @copy($tmppath, $dest_file);
|
|
|
+ if (!copy($tmppath, $dest_file)) {
|
|
|
+ return self::clean_up($tmppath, $result);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
- @copy($tmppath, $dest_file);
|
|
|
+ if (!copy($tmppath, $dest_file)) {
|
|
|
+ return self::clean_up($tmppath, $result);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- self::unlink_tmpfiles($tmppath);
|
|
|
-
|
|
|
$result["ret"] = true;
|
|
|
$result["file"] = $dest_file;
|
|
|
- return $result;
|
|
|
+ return self::clean_up($tmppath, $result);
|
|
|
}
|
|
|
}
|
|
|
|