Skip to content
This repository was archived by the owner on Jan 9, 2022. It is now read-only.

Commit 9103bf3

Browse files
author
Sascha Ende
committed
* **17.07.2018** - [0.9.11] Finalized Upload() Features
1 parent 84d2cdc commit 9103bf3

File tree

3 files changed

+138
-52
lines changed

3 files changed

+138
-52
lines changed

Classes/Utilities/Upload.php

Lines changed: 97 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,90 +2,145 @@
22

33
namespace SaschaEnde\T3helpers\Utilities;
44

5-
use TYPO3\CMS\Core\Resource\ResourceFactory;
5+
use t3h\t3h;
66
use TYPO3\CMS\Core\SingletonInterface;
7+
use TYPO3\CMS\Core\Utility\File\BasicFileUtility;
8+
use TYPO3\CMS\Core\Utility\GeneralUtility;
79

810
class Upload implements UploadInterface, SingletonInterface {
911

1012
protected $maxFiles = null;
11-
protected $allowedFiletypes = ['jpg','jpeg','zip','rar','pdf','gif'];
13+
protected $allowedFiletypes = ['jpg', 'jpeg', 'zip', 'rar', 'pdf', 'gif'];
1214
protected $maxFilesize = null;
13-
protected $maxFilesizeTotal = null;
1415
protected $targetFolder = '';
1516
protected $autoFilenames = true;
1617
protected $FILES = [];
1718

1819
/**
20+
* Set allowed filetypes
1921
* @param $filetypes
2022
* @return $this
2123
*/
22-
public function setAllowedFiletypes($filetypes){
24+
public function setAllowedFiletypes($filetypes) {
2325
$this->allowedFiletypes = $filetypes;
2426
return $this;
2527
}
2628

2729
/**
30+
* Set max filesize, allowed for each file
2831
* @param $size
2932
* @return $this
3033
*/
31-
public function setMaxFilesize($size){
34+
public function setMaxFilesize($size) {
3235
$this->maxFilesize = $size;
3336
return $this;
3437
}
3538

3639
/**
37-
* @param $size
40+
* Enable autonaming of uploaded files with hash values
41+
* @param $setting
3842
* @return $this
3943
*/
40-
public function setMaxFilesizeTotal($size){
41-
$this->maxFilesizeTotal = $size;
44+
public function setAutofilenames($setting) {
45+
$this->autoFilenames = $setting;
4246
return $this;
4347
}
4448

4549
/**
46-
* @param $setting
47-
* @return $this
50+
* Check uploaded files and set them for upload
51+
* @return array
4852
*/
49-
public function setAutofilenames($setting){
50-
$this->autoFilenames = $setting;
51-
return $this;
52-
}
53+
public function check() {
54+
55+
/** @var BasicFileUtility $BasicFileUtility */
56+
$BasicFileUtility = t3h::injectClass(BasicFileUtility::class);
57+
58+
$Errors = [];
59+
60+
if ($this->maxFiles != null && count($FILES['name']) > $this->maxFiles) {
61+
$Errors[] = [
62+
'error' => 'count'
63+
];
64+
}
65+
66+
67+
foreach ($_FILES as $ext => $FILES) {
68+
foreach ($FILES['name'] as $prop => $value) {
69+
70+
if ($FILES['size'][$prop] == 0) {
71+
continue;
72+
}
5373

54-
public function check(){
55-
foreach($_FILES as $ext=>$FILES){
56-
foreach($FILES['name'] as $prop=>$value){
57-
$this->FILES[$prop] = [
58-
'name' => $FILES['name'][$prop],
59-
'type' => $FILES['type'][$prop],
60-
'tmp_name' => $FILES['tmp_name'][$prop],
61-
'error' => $FILES['error'][$prop],
62-
'size' => $FILES['size'][$prop],
63-
'pathinfo' => pathinfo($FILES['name'][$prop]),
74+
// get pathinfo for this file
75+
$pathinfo = pathinfo($FILES['name'][$prop]);
76+
77+
if ($this->autoFilenames) {
78+
$targetFilename = sha1($FILES['name'][$prop] . uniqid()) . time() . '.' . $pathinfo['extension'];
79+
} else {
80+
$targetFilename = \TYPO3\CMS\Core\Resource\Driver\LocalDriver::sanitizeFileName($FILES['name'][$prop]);
81+
}
82+
83+
$uploadfile = [
84+
'targetFilename' => $targetFilename,
85+
'name' => $FILES['name'][$prop],
86+
'type' => $FILES['type'][$prop],
87+
'tmp_name' => $FILES['tmp_name'][$prop],
88+
'error' => $FILES['error'][$prop],
89+
'size' => $FILES['size'][$prop],
90+
'pathinfo' => $pathinfo,
6491
];
92+
$this->FILES[$prop] = $uploadfile;
93+
94+
// Checks: Dateityp
95+
if (!in_array(strtolower($pathinfo['extension']), $this->allowedFiletypes)) {
96+
$Errors[] = [
97+
'error' => 'type',
98+
'file' => $uploadfile
99+
];
100+
}
101+
102+
// Dateigröße maximal
103+
if ($this->maxFilesize != null && $uploadfile['size'] > $this->maxFilesize) {
104+
$Errors[] = [
105+
'error' => 'size',
106+
'file' => $uploadfile
107+
];
108+
}
109+
65110
}
66111
}
67-
debug($this->FILES);
112+
return $Errors;
68113
}
69114

70-
public function execute($target_folder){
71-
foreach($this->FILES as $file){
72-
$this->move(
73-
$file['tmp_name'],
74-
$target_folder,
75-
$file['name']
76-
);
77-
}
115+
/**
116+
* Get the list of files that will be uploaded
117+
* @return array
118+
*/
119+
public function getFiles() {
120+
return $this->FILES;
78121
}
79122

80-
private function move($tmp_file,$target_folder,$target_filename){
81-
$resourceFactory = ResourceFactory::getInstance();
82-
$storage = $resourceFactory->getDefaultStorage();
83-
$newFile = $storage->addFile(
84-
$tmp_file,
85-
$storage->getFolder($target_folder),
86-
$target_filename
87-
);
88-
return $newFile;
123+
/**
124+
* Upload files to target directory
125+
* @param $target_folder
126+
*/
127+
public function execute($target_folder = false) {
128+
$results = [];
129+
foreach ($this->FILES as $file) {
130+
if($target_folder == false){
131+
$results[] = GeneralUtility::upload_to_tempfile($file['targetFilename']);
132+
}else{
133+
$results[] = GeneralUtility::upload_copy_move($file['tmp_name'], PATH_site . '/fileadmin/' . $target_folder . '/' . $file['targetFilename']);
134+
135+
// Ordner neu indexieren
136+
$resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
137+
$defaultStorage = $resourceFactory->getDefaultStorage();
138+
$folder = $defaultStorage->getFolder($target_folder);
139+
$files = $defaultStorage->getFilesInFolder($folder); // Aufruf dieser Zeile sorgt automatisch für eine Reindexierung des Ordners
140+
}
141+
142+
}
143+
return $results;
89144
}
90145

91146
}

Classes/Utilities/UploadInterface.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,42 @@
55
interface UploadInterface {
66

77
/**
8+
* Set allowed filetypes
89
* @param $filetypes
910
* @return $this
1011
*/
1112
public function setAllowedFiletypes($filetypes);
1213

1314
/**
15+
* Set max filesize, allowed for each file
1416
* @param $size
1517
* @return $this
1618
*/
1719
public function setMaxFilesize($size);
1820

1921
/**
20-
* @param $size
21-
* @return $this
22-
*/
23-
public function setMaxFilesizeTotal($size);
24-
25-
/**
22+
* Enable autonaming of uploaded files with hash values
2623
* @param $setting
2724
* @return $this
2825
*/
2926
public function setAutofilenames($setting);
3027

28+
/**
29+
* Check uploaded files and set them for upload
30+
* @return array
31+
*/
3132
public function check();
3233

33-
public function execute($target_folder);
34+
/**
35+
* Get the list of files that will be uploaded
36+
* @return array
37+
*/
38+
public function getFiles();
39+
40+
/**
41+
* Upload files to target directory. If no target is given, temp folder will be used
42+
* @param $target_folder
43+
*/
44+
public function execute($target_folder = false);
3445

3546
}

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ Helpers for Extbase: Simple and easy functions that make your TYPO3 life with ex
77

88
# Dev version (not released yet)
99

10-
* **25.06.2018** - [0.9.11] AbstractDatamapper Hook (after updating/adding database entries hooks)
11-
* **21.06.2018** - [0.9.11] Configuration::setExtension() -> return $this
10+
nothing
1211

1312
# Changelog
1413

14+
* **17.07.2018** - [0.9.11] Finalized Upload() Features
15+
* **17.07.2018** - [0.9.11] Bugfix, save session Data (by Markus A.)
16+
* **25.06.2018** - [0.9.11] AbstractDatamapper Hook (after updating/adding database entries hooks)
17+
* **21.06.2018** - [0.9.11] Configuration::setExtension() -> return $this
1518
* **14.06.2018** - [0.9.10] Abstract eID Dispatcher, check Classes/Examples/EidExample.php
1619
* **05.06.2018** - [0.9.9] Interface implements for Utility Classes
1720
* **05.06.2018** - [0.9.9] + Datastorage()
1821
* **01.06.2018** - [0.9.9] Settings()->getExtension($extensionName, $part = 'settings')
1922
* **24.05.2018** - [0.9.9] Database()->getQuerybuilder($table, $addFrom = true)
20-
2123
* **23.05.2018** - [0.9.8] Data()->xmlToArray($xmldata)
2224
* **22.04.2018** - [0.9.6] Filesystem->fileExists($folder,$fileName)
2325
* **04.04.2018** - models and repositories for pages, contents / content element object viewhelper / Filesystem::getFileObjectByID($id) / phpdoc comments for Filesystem / Link()->Uri() / Link() is deprecated / t3h::Category()
@@ -69,7 +71,25 @@ $filePath = t3h::Filesystem()->getFileExtPath('t3helpers','Resources/Private/Lib
6971
````
7072

7173

74+
## File Uploads in Frontend: Upload example
7275

76+
````
77+
// Dateinamen beim Hochladen automatisch generieren und nicht den Originaldateinamen verwenden
78+
t3h::Upload()->setAutofilenames(true);
79+
// Erlaubte Dateiendungen
80+
t3h::Upload()->setAllowedFiletypes(['csv','gz']);
81+
// Initialisierung der hochgeladenen Dateien und Prüfung
82+
$errors = t3h::Upload()->check();
83+
debug($errors);
84+
// Dateien erhalten
85+
if(count($errors) <= 0){
86+
$files = t3h::Upload()->getFiles();
87+
debug($files);
88+
// Upload und Dateien verschieben in Zielverzeichnis
89+
$results = t3h::Upload()->execute('user_upload/meinordner');
90+
debug($results);
91+
}
92+
````
7393

7494

7595

0 commit comments

Comments
 (0)