Skip to content

Instantly share code, notes, and snippets.

@xperseguers
Last active April 4, 2023 07:56
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save xperseguers/9076406 to your computer and use it in GitHub Desktop.
Save xperseguers/9076406 to your computer and use it in GitHub Desktop.
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
Copy link

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);

@hda-typo3
Copy link

Hi Xavier, i tried this controller, cause i hoped to find a solution for TYPO3 11 fe_upload, but all i got is "Exception while property mapping at property path "image": Property "name" was not found in target object of type "TYPO3\CMS\Extbase\Domain\Model\FileReference"." Something changed with the 11 and there is no extension (incl. Helmut Hummel upload_example ) / tutorial or something else.
Best regards
Michael

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