Extbase - Datei Upload ohne FAL
Aus Wikizone
Version vom 22. Oktober 2015, 12:57 Uhr von 149.172.224.79 (Diskussion)
basiert auf https://forum.typo3.org/index.php/t/205140/ (Achtung z.t. nutzt dieses Beispiel deprecated Funktionen in 6.2, das Beispiel unten ist angepasst)
Controller
<?php
namespace Geobit\Rmaupload\Controller;
use \TYPO3\CMS\Extbase\Utility\DebuggerUtility;
/***************************************************************
*
* Copyright notice
*
* (c) 2015 Stephan Schlegel <schlegel@geo-bit.de>, Geo-bit
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* UploadsController
*/
class UploadsController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
const UPLOAD_TARGET = 'fileadmin/user_upload/rma/';
/**
* uploadsRepository
*
* @var \Geobit\Rmaupload\Domain\Repository\UploadsRepository
* @inject
*/
protected $uploadsRepository = NULL;
/**
* action new
*
* @param \Geobit\Rmaupload\Domain\Model\Uploads $newUploads
* @ignorevalidation $newUploads
* @return void
*/
public function newAction(\Geobit\Rmaupload\Domain\Model\Uploads $newUploads = NULL) {
$this->view->assign('newUploads', $newUploads);
}
/**
* action create
*
* @return void
*/
public function createAction() {
$args = $this->request->getArguments();
DebuggerUtility::var_dump($args);
if( $this->saveFile($args['file'])){
$this->addFlashMessage('Upload was successful', '', \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR);
}
$this->redirect('new');
}
/**
* Stores the file
*
* @param \array $file
* @return boolean $success
*/
public function saveFile($file) {
$success = true;
$date = new \DateTime();
$basicFileFunctions = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Utility\File\BasicFileUtility');
$types = Array('text/csv' => 'csv', 'image/png' => 'png', 'image/jpeg' => 'jpg', 'image/jpeg' => 'jpeg', 'image/gif' => 'gif');
$absDestFilePath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(self::UPLOAD_TARGET);
$cleanFileName = $basicFileFunctions->cleanFileName($date->getTimestamp().'_'.$file['name']);
$fileName = $absDestFilePath.$cleanFileName;
//DebuggerUtility::var_dump($fileName);
//die();
if ($file['size'] <= 0) {
$this->addFlashMessage('Upload failed', '', \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR);
$success = FALSE;
} elseif( !array_key_exists($file['type'], $types) ) {
$this->addFlashMessage('Invalid file format', '', \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR);
$success = FALSE;
} else {
if ( \TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move( $file['tmp_name'], $fileName) ) {
$this->addFlashMessage('File '.$fileName.' saved', '', \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR);
//$this->file = basename($fileName);
}else{
$this->addFlashMessage('File '.$fileName.' could not be saved.', '', \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR);
$success = FALSE;
}
}
return $success;
}
}
Dann kannst du im Formular das ganz einfach hochladen mit
<f:form.upload property="daten.userimage" />
Ist sicher nicht perfekt. Zum Beispiel zum error reporting etc.
Aber vielleicht hilft es. Denke man kann das ganze sicher noch umbauen das es auch mit FAL funktioniert.