Skip to content

Instantly share code, notes, and snippets.

@astehlik
Forked from xperseguers/controller.php
Created February 18, 2014 18:57
Show Gist options
  • Save astehlik/9077441 to your computer and use it in GitHub Desktop.
Save astehlik/9077441 to your computer and use it in GitHub Desktop.
/**
* 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)
}
/**
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment