Ich hab schonmal hier: https://typo3blogger.de/extbase-file-upload/ etwas zu dazu geschrieben.
Da das allerdings ab und zu in Foren nochmal Thema ist und der alte Artikel mit der heißen Nadel gestrickt war, gibt’s noch nen kleines Followup:
Code sagt mehr als tausend Worte:
Usage im Controller:
if ($uploadedFile = $this->handleFileUpload('fooBarFile', 'uploads/tx_hasteNichtGesehen') { ...was auch immer } |
Controller Code
/** * Forwards back to referrer with setting an error * * @param $fieldName * @param $errorMessage * @param $errorCode * @return void */ protected function throwBackWithError($fieldName, $errorMessage, $errorCode) { $error = $this->objectManager->get('Tx_Extbase_MVC_Controller_ArgumentError' , $fieldName); $error->addErrors(array( $this->objectManager->get('Tx_Extbase_Validation_Error', $errorMessage,$errorCode) ) ); $this->request->setErrors(array($error)); $referrer = $this->request->getInternalArgument('__referrer'); $this->forward($referrer['actionName'], $referrer['controllerName'], $referrer['extensionName'], $this->request->getArguments()); } /** * Handles file upload * * @param $fieldName * @param $uploadFolder * @return bool|string */ protected function handleFileUpload($fieldName, $uploadFolder) { $pluginNamespace = $this->objectManager->get('Tx_Extbase_Service_ExtensionService') ->getPluginNamespace($this->request->getControllerExtensionName(), $this->request->getPluginName()); if (!$_FILES || !isset($_FILES[$pluginNamespace]['name'][$fieldName]) || $_FILES[$pluginNamespace]['error'][$fieldName] === UPLOAD_ERR_NO_FILE) { return FALSE; } if ($_FILES[$pluginNamespace]['error'][$fieldName] === UPLOAD_ERR_FORM_SIZE || $_FILES[$pluginNamespace]['error'][$fieldName] === UPLOAD_ERR_INI_SIZE) { $this->throwBackWithError($fieldName, 'File upload size exceeded', 1386779047); } $uniqueFileName = $this->objectManager->get('t3lib_basicFileFunctions')->getUniqueName( $_FILES[$pluginNamespace]['name'][$fieldName], t3lib_div::getFileAbsFileName($uploadFolder) ); t3lib_div::upload_copy_move( $_FILES[$pluginNamespace]['tmp_name'][$fieldName], $uniqueFileName ); return basename($uniqueFileName); } |
Für die Größenbeschränkung ist noch folgendes sinnvoll:
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" /> <f:form.upload name="fooBarFile" /> |
Mehr zum „MAX_FILE_SIZE“ Feld gibts hier:
http://php.net/manual/de/features.file-upload.post-method.php
Und zu den Errorcodes hier:
http://www.php.net/manual/en/features.file-upload.errors.php
Das ist natürlich nicht die reine Lehre aber ein pragmatisch, gangbarer Ansatz.
Das ganze funktioniert nur bis TYPO3 6.1, weil dort das $_FILES Array durch ExtBase umgeschrieben wird. Ich hatte bisher noch nicht damit zu tun, aber sobald das der Fall ist gibt’s nen Artikel dazu.
Nachtrag 12.12.2013:
Wie in den Kommentaren schon angemerkt ist es natürlich sinnvoll sowohl file type als auch die Größe nochmal serverseitig zu validieren. Das kann dann so aussehen:
/** + Handles file upload * * @param $fieldName * @param $uploadFolder * @param int $maxFileSize * @param array $allowedFileTypes * @return bool|string */ protected function handleFileUpload($fieldName, $uploadFolder, $maxFileSize=1048576, $allowedFileTypes = array('application/pdf')) { $pluginNamespace = $this->objectManager->get('Tx_Extbase_Service_ExtensionService') ->getPluginNamespace($this->request->getControllerExtensionName(), $this->request->getPluginName()); if (!$_FILES || !isset($_FILES[$pluginNamespace]['name'][$fieldName]) || $_FILES[$pluginNamespace]['error'][$fieldName] === UPLOAD_ERR_NO_FILE) { return FALSE; } if ($_FILES[$pluginNamespace]['error'][$fieldName] === UPLOAD_ERR_FORM_SIZE || $_FILES[$pluginNamespace]['error'][$fieldName] === UPLOAD_ERR_INI_SIZE || $_FILES[$pluginNamespace]['size'][$fieldName] > $maxFileSize) { $this->throwBackWithError($fieldName, 'File upload size exceeded', 1386779047); } if (!in_array(mime_content_type($_FILES[$pluginNamespace]['tmp_name'][$fieldName]), $allowedFileTypes)) { $this->throwBackWithError($fieldName, 'File type not allowed', 1386855251); } $uniqueFileName = $this->objectManager->get('t3lib_basicFileFunctions')->getUniqueName( $_FILES[$pluginNamespace]['name'][$fieldName], t3lib_div::getFileAbsFileName($uploadFolder) ); t3lib_div::upload_copy_move( $_FILES[$pluginNamespace]['tmp_name'][$fieldName], $uniqueFileName ); return basename($uniqueFileName); } |