Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Upload files in TYPO3 from Frontend
/**
* Upload files.
*
* @return void
*/
public function uploadAction() {
$overwriteExistingFiles = TRUE;
$data = array();
$namespace = key($_FILES);
$targetFalDirectory = '1:/_temp_/';
// Register every upload field from the form:
$this->registerUploadField($data, $namespace, 'myimage', $targetFalDirectory);
// Initializing:
/** @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility $fileProcessor */
$fileProcessor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Utility\\File\\ExtendedFileUtility');
$fileProcessor->init(array(), $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
$fileProcessor->setActionPermissions(array('addFile' => TRUE));
$fileProcessor->dontCheckForUnique = $overwriteExistingFiles ? 1 : 0;
// Actual upload
$fileProcessor->start($data);
$result = $fileProcessor->processData();
// Do whatever you want with $result (array of File objects)
foreach ($result['upload'] as $files) {
/** @var \TYPO3\CMS\Core\Resource\File $file */
$file = $files[0]; // Single element array due to the way we registered upload fields
}
}
/**
* Registers an uploaded file for TYPO3 native upload handling.
*
* @param array &$data
* @param string $namespace
* @param string $fieldName
* @param string $targetDirectory
* @return void
*/
protected function registerUploadField(array &$data, $namespace, $fieldName, $targetDirectory = '1:/_temp_/') {
if (!isset($data['upload'])) {
$data['upload'] = array();
}
$counter = count($data['upload']) + 1;
$keys = array_keys($_FILES[$namespace]);
foreach ($keys as $key) {
$_FILES['upload_' . $counter][$key] = $_FILES[$namespace][$key][$fieldName];
}
$data['upload'][$counter] = array(
'data' => $counter,
'target' => $targetDirectory,
);
}
<f:form action="upload" enctype="multipart/form-data">
<f:form.upload name="myimage" />
<f:form.submit value="GO" />
</f:form>
@glucka

This comment has been minimized.

Copy link

@glucka glucka commented Apr 2, 2020

for typo3 v9
//$fileProcessor->init(array(), $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
//$fileProcessor->dontCheckForUnique = $overwriteExistingFiles ? 1 : 0;
$fileProcessor->setExistingFilesConflictMode(\TYPO3\CMS\Core\Resource\DuplicationBehavior::REPLACE);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment