[TYPO3-mvc] Re: Move uploaded File utility class
Nils Blattner
nb at cabag.ch
Fri Feb 19 14:40:39 CET 2010
Because extbase so far does not directly support to move uploaded files
and such, I wrote a little utility class.
I suppose it could have been made more model, with a class that
represents the temp file etc. But this does the trick.
$this->createPluginName holds 'tx_cabagsteps_pi2' and the
$this->upload... hold the absolute/relative upload paths respectively of
course.
About the path array:
array('container', 'team', 'picture')
translates to
$settings['container']['team']['picture']
which is
$_FILES['tx_cabagsteps_pi2']['container']['team']['picture']
originally.
Note that resolveRecordOfFile() only returns an array with the keys
'name' and 'tmp_name' as those are the only ones needed to move the
file. The others are ignored to save a (tiny) little bit of performance.
Should any more info's be needed you can just set the $var parameter
with the keys you'd like.
$this->fileUtility =
t3lib_div::makeInstance('Tx_CabagSteps_Utility_File');
$this->fileUtility->injectUploadDirectories($this->uploadsDir,
$this->relativeUploadsDir);
// validate and move the uploaded file
$file =
$this->fileUtility->moveUploadedFile($_FILES[$this->createPluginName],
array('container', 'team', 'picture'));
<?php
/***************************************************************
* Copyright notice
*
* (c) 2009 Nils Blattner <nb at cabag.ch>
* 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 2 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!
***************************************************************/
/**
* The File utility class
*
* @version $Id:$
* @license http://opensource.org/licenses/gpl-license.php GNU Public
License, version 2
*/
class Tx_CabagSteps_Utility_File {
/**
* @var string Path to the uploads/tx_cabagsteps/
*/
public $uploadsDir;
/**
* @var string Relative path to the extension upload dir.
*/
public $relativeUploadsDir = '/uploads/tx_cabagsteps/';
public function __construct() {
$this->uploadsDir = PATH_site . "uploads/tx_cabagsteps/";
}
public function injectUploadDirectories($absolute, $relative = null) {
if (!empty($absolute)) $this->uploadsDir = $absolute;
if ($relative != null) $this->relativeUploadsDir = $relative;
}
/**
* Moves the uploaded file to the upload directory and returns its new
path.
*
* @param array $settings Settings array as returned by
$_FILES['tx_cabagsteps_pi2']
* @param array $path Array of the form array('level0name',
'level1name', ...)
* @param string $allowed Regex pattern of allowed file extensions,
default to picture extensions
* @return mixed Returns false if it failed, the relative path to the
file otherwise.
*/
public function moveUploadedFile($settings, $path, $allowed =
"(jpg)|(png)|(gif)") {
$record = $this->resolveRecordOfFile($settings, $path);
$matches = array();
// no fancy file names
if (!preg_match('/^([a-zA-Z0-9\-_\.]+)\.('.$allowed.')$/ix',
$record['name'], $matches)) {
return false;
}
$base = $matches[1];
$ext = $matches[2];
$file = $base . '.' . $ext;
$c = 1;
//echo 'done ... checking for existing files ...';
// finds the spot for the new file (so no old one is overwritten)
while (file_exists($this->uploadsDir . $file)) {
$file = $base . '-' . $c . '.' . $ext;
$c++;
}
//echo 'done ... moving file ...';
$t_file = t3lib_div::upload_copy_move($record['tmp_name'],
$this->uploadsDir . $file);
//print_r($t_file);
if (!$t_file) return false;
//echo 'done ... returning';
return $this->relativeUploadsDir . $file;
}
/**
* Resolves the record of a file as given by $_FILES
*
* @param array $settings Settings array as returned by
$_FILES['tx_cabagsteps_pi2']
* @param array $path Array of the form array('level0name',
'level1name', ...)
* @param array $vars Array of properties to take over to the new record.
* @return mixed Returns false if it failed, the record otherwise.
*/
public function resolveRecordOfFile($settings, $path, $vars =
array('name', 'tmp_name', )) {
if (empty($settings) || !is_array($settings) || empty($path) ||
!is_array($path)) {
return false;
}
//$vars = array('name', 'type', 'tmp_name', 'error', 'size', );
$record = array();
// creates an entry in $record for each of the $vars with the
respective path
foreach ($vars as $key) {
$t_setting = $settings[$key];
foreach ($path as $p) {
if (empty($t_setting)) return false;
$t_setting = $t_setting[$p];
}
$record[$key] = $t_setting;
}
return $record;
}
}
?>
More information about the TYPO3-project-typo3v4mvc
mailing list