170 Mime $fileMime =
null 172 $this->_setUploadFileId($fileId);
173 if (!file_exists($this->_file[
'tmp_name'])) {
175 throw new \Exception(
'The file was not uploaded.',
$code);
177 $this->_fileExists =
true;
203 public function save($destinationFolder, $newFileName =
null)
206 $this->validateDestination($destinationFolder);
208 $this->_result =
false;
209 $destinationFile = $destinationFolder;
210 $fileName = isset($newFileName) ? $newFileName : $this->_file[
'name'];
212 if ($this->_enableFilesDispersion) {
215 $this->_dispretionPath = static::getDispersionPath(
$fileName);
217 $this->_createDestinationFolder($destinationFile);
220 if ($this->_allowRenameFiles) {
222 static::_addDirSeparator($destinationFile) .
$fileName 226 $destinationFile = static::_addDirSeparator($destinationFile) .
$fileName;
229 $this->_result = $this->
_moveFile($this->_file[
'tmp_name'], $destinationFile);
230 }
catch (\Exception $e) {
232 if (file_exists($destinationFile)) {
233 $this->_result =
true;
239 if ($this->_result) {
240 if ($this->_enableFilesDispersion) {
241 $fileName = str_replace(
'\\',
'/', self::_addDirSeparator($this->_dispretionPath)) .
$fileName;
244 $this->_uploadedFileDir = $destinationFolder;
246 $this->_result[
'path'] = $destinationFolder;
262 private function validateDestination($destinationFolder)
264 if ($this->_allowCreateFolders) {
265 $this->_createDestinationFolder($destinationFolder);
269 throw new \Exception(
'Destination folder is not writable or does not exists.');
296 return move_uploaded_file($tmpPath, $destPath);
298 return rename($tmpPath, $destPath);
310 if ($this->_fileExists ===
false) {
316 throw new \Exception(
'Disallowed file type.');
319 foreach ($this->_validateCallbacks as
$params) {
320 if (is_object(
$params[
'object'])
336 return $this->_fileExists ? pathinfo($this->_file[
'name'], PATHINFO_EXTENSION) :
'';
350 $this->_validateCallbacks[$callbackName] = [
'object' => $callbackObject,
'method' => $callbackMethod];
363 if (isset($this->_validateCallbacks[$callbackName])) {
364 unset($this->_validateCallbacks[$callbackName]);
380 if (preg_match(
'/^_+$/',
$fileInfo[
'filename'])) {
394 if ($this->_caseInsensitiveFilenames) {
408 if (substr($dir, -1) !=
'/') {
423 if (count($validTypes) > 0) {
424 if (!in_array($this->_getMimeType(), $validTypes)) {
439 return $this->_uploadedFileName;
451 $this->_allowCreateFolders = $flag;
464 $this->_allowRenameFiles = $flag;
477 $this->_enableFilesDispersion = $flag;
489 $this->_caseInsensitiveFilenames = $flag;
502 $this->_allowedExtensions[] = strtolower(
$extension);
515 if (!is_array($this->_allowedExtensions) || empty($this->_allowedExtensions)) {
519 return in_array(strtolower(
$extension), $this->_allowedExtensions);
527 private function _getMimeType()
529 return $this->fileMime->getMimeType($this->_file[
'tmp_name']);
540 private function _setUploadFileId($fileId)
542 if (is_array($fileId)) {
543 $this->_uploadType = self::MULTIPLE_STYLE;
544 $this->_file = $fileId;
546 if (empty($_FILES)) {
547 throw new \Exception(
'$_FILES array is empty');
550 preg_match(
"/^(.*?)\[(.*?)\]$/", $fileId, $file);
552 if (is_array($file) && count($file) > 0 && count($file[0]) > 0 && count($file[1]) > 0) {
554 $this->_uploadType = self::MULTIPLE_STYLE;
556 $fileAttributes = $_FILES[$file[0]];
559 foreach ($fileAttributes as $attributeName => $attributeValue) {
560 $tmpVar[$attributeName] = $attributeValue[$file[1]];
563 $fileAttributes = $tmpVar;
564 $this->_file = $fileAttributes;
565 }
elseif (!empty($fileId) && isset($_FILES[$fileId])) {
566 $this->_uploadType = self::SINGLE_STYLE;
567 $this->_file = $_FILES[$fileId];
568 }
elseif ($fileId ==
'') {
569 throw new \Exception(
'Invalid parameter given. A valid $_FILES[] identifier is expected.');
581 private function _createDestinationFolder($destinationFolder)
583 if (!$destinationFolder) {
587 if (substr($destinationFolder, -1) ==
'/') {
588 $destinationFolder = substr($destinationFolder, 0, -1);
591 if (!(@
is_dir($destinationFolder)
592 || @
mkdir($destinationFolder, 0777,
true)
594 throw new \Exception(
"Unable to create directory '{$destinationFolder}'.");
608 if (file_exists($destinationFile)) {
611 while (file_exists(
$fileInfo[
'dirname'] .
'/' . $baseName)) {
615 $destFileName = $baseName;
620 return $destFileName;
632 return self::getDispersionPath(
$fileName);
645 $dispertionPath =
'';
646 while ($char < 2 && $char < strlen(
$fileName)) {
647 if (empty($dispertionPath)) {
650 $dispertionPath = self::_addDirSeparator(
656 return $dispertionPath;
removeValidateCallback($callbackName)
addValidateCallback($callbackName, $callbackObject, $callbackMethod)
elseif(isset( $params[ 'redirect_parent']))
static getDispretionPath($fileName)
checkAllowedExtension($extension)
static getCorrectFileName($fileName)
setAllowedExtensions($extensions=[])
$_caseInsensitiveFilenames
__construct( $fileId, Mime $fileMime=null)
setFilenamesCaseSensitivity($flag)
rename($oldPath, $newPath, DriverInterface $targetDriver=null)
correctFileNameCase($fileName)
setFilesDispersion($flag)
static _addDirSeparator($dir)
setAllowRenameFiles($flag)
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
setAllowCreateFolders($flag)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
static getNewFileName($destinationFile)
checkMimeType($validTypes=[])
save($destinationFolder, $newFileName=null)
static getDispersionPath($fileName)
_moveFile($tmpPath, $destPath)