Extbase - Datei Upload ohne FAL: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „https://forum.typo3.org/index.php/t/205140/ Habe einmal folgende Lösung gefunden: Nutzt aber kein FAL! in deinem Domain model wo das Bild zugeordnet sein so…“)
 
 
(Eine dazwischenliegende Version desselben Benutzers wird nicht angezeigt)
Zeile 1: Zeile 1:
https://forum.typo3.org/index.php/t/205140/
+
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)
  
Habe einmal folgende Lösung gefunden: Nutzt aber kein FAL!
+
Controller
 +
<syntaxhighlight lang="php">
 +
<?php
 +
namespace Geobit\Rmaupload\Controller;
 +
use \TYPO3\CMS\Extbase\Utility\DebuggerUtility;
  
in deinem Domain model wo das Bild zugeordnet sein soll:
+
/***************************************************************
<syntaxhighlight lang="php">
+
*
const UPLOAD_TARGET = 'fileadmin/userimages/';
+
* 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!
 +
***************************************************************/
  
 
/**
 
/**
* Sets the userimage
+
* UploadsController
*
+
*/
* @param \array $userimage
+
class UploadsController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
* @return void
+
const UPLOAD_TARGET = 'fileadmin/user_upload/rma/';
*/
+
/**
public function setUserimage($userimage) {
+
* uploadsRepository
$date = new \DateTime();
+
*
 +
* @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);
 +
}
  
$basicFileFunctions = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Utility\File\BasicFileUtility');
+
/**
$types = Array('image/png' => 'png', 'image/jpeg' => 'jpg', 'image/jpeg' => 'jpeg', 'image/gif' => 'gif');
+
* 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');
 +
}
  
$fileName = $basicFileFunctions->getUniqueName(
+
/**
$basicFileFunctions->cleanFileName($date->getTimestamp().'_'.$userimage['name']),
+
* Stores the file
\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(self::UPLOAD_TARGET)
+
*
);
+
* @param \array $file
if ($userimage['size'] <= 0) {
+
* @return boolean $success
$error = 'Upload fehlgeschlagen';
+
*/
} elseif( !array_key_exists($userimage['type'], $types) ) {
+
public function saveFile($file) {
$error = 'Das Bild hat ein ungültiges Format';
+
$success = true;
} else {
+
$date = new \DateTime();
if ( \TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move( $userimage['tmp_name'], $fileName) ) {
+
$basicFileFunctions = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Utility\File\BasicFileUtility');
$this->userimage = basename($fileName);
+
$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;
 +
}
  
 
}
 
}

Aktuelle Version vom 22. Oktober 2015, 12:57 Uhr

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.