Skip to content

Commit

Permalink
feat(Upload): 增加 path 属性,用于指定上传文件的完整路径
Browse files Browse the repository at this point in the history
`path` 用于替代 `dir` 和 `fileName`,指定后 `dir` 和 `fileName` 将失效
  • Loading branch information
twinh committed Feb 27, 2022
1 parent 63703ca commit 6842a8b
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions lib/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ class Upload extends IsImage
*/
protected $fileName;

/**
* The path (without extension) to save file, if specified, $dir and $fileName will be ignored
*
* @var string
*/
protected $path;

/**
* Whether check if the upload file is valid image or not
*
Expand Down Expand Up @@ -328,18 +335,27 @@ protected function saveFile($uploadedFile)
$ext = $this->getExt();
$fullExt = $ext ? '.' . $ext : '';

if ($this->fileName) {
$fileName = $this->fileName;
if ($this->path) {
$dir = dirname($this->path);
if (!is_dir($dir)) {
mkdir($dir, 0700, true);
}

$this->file = $this->path . $fullExt;
} else {
$fileName = substr($uploadedFile['name'], 0, strlen($uploadedFile['name']) - strlen($fullExt));
}
if ($this->fileName) {
$fileName = $this->fileName;
} else {
$fileName = substr($uploadedFile['name'], 0, strlen($uploadedFile['name']) - strlen($fullExt));
}

$this->file = $this->dir . '/' . $fileName . $fullExt;
if (!$this->overwrite) {
$index = 1;
while (is_file($this->file)) {
$this->file = $this->dir . '/' . $fileName . '-' . $index . $fullExt;
++$index;
$this->file = $this->dir . '/' . $fileName . $fullExt;
if (!$this->overwrite) {
$index = 1;
while (is_file($this->file)) {
$this->file = $this->dir . '/' . $fileName . '-' . $index . $fullExt;
++$index;
}
}
}

Expand Down

0 comments on commit 6842a8b

Please sign in to comment.