[TYPO3-german] File upload in Extension

David Bruchmann david at bruchmann-web.de
Sat Jan 10 17:23:19 CET 2009


Hallo René,


Nachfolgend habe ich mal ein Snippet, das bei mir funktioniert.
Kommentare lasse ich mal extra drin.
Wie der Array $_FILES sich zusammensetzt, geht aus dem Kommentar hervor, ist
aber abhängig davon wie das Formular die Daten übermittelt.

Am Besten als PHP abspeichern, dann kannst Du es im Editor besser ansehen.

Gruß
David

________________________

		/*
		$_FILES  = ARRAY (
			'tx_YOUR_EXTENSION_pi1' => ARRAY (
				'name'		=> ARRAY ('image' =>
Array('0002210.JPG','0002210.JPG','0002210.JPG','0002210.JPG','0002210.JPG')
),
				'type'		=> ARRAY ('image' =>
Array('image/jpeg','image/jpeg','image/jpeg','image/jpeg','image/jpeg')),
				'tmp_name'	=> ARRAY ('image' =>
Array('N:\apache_2008\temp\php55.tmp','N:\apache_2008\temp\php55.tmp','N:\ap
ache_2008\temp\php55.tmp','N:\apache_2008\temp\php55.tmp','N:\apache_2008\te
mp\php55.tmp')),
				'error'		=> ARRAY ('image' =>
Array('0','0','0','0','0'),
				'size'		=> ARRAY ('image' =>
Array('28606','28606','28606','28606','28606'),
			)
		);
		*/
		$newData['images'] = array();
		$emptyString = '';
		for ($n=1;$n <= $this->maxImgAmount;$n++) {
			$tmpImage = '';

			$uploadedFileName = ($_FILES &&
$_FILES['tx_YOUR_EXTENSION_pi1']['tmp_name']['image'][$n]) ?
$_FILES['tx_YOUR_EXTENSION_pi1']['tmp_name']['image'][$n] : '';
			if ($uploadedFileName) {

				// upload to temp-file
				$tmpName =
t3lib_div::upload_to_tempfile($uploadedFileName);

				$this->fileFunc =
t3lib_div::makeInstance('t3lib_basicFileFunctions');
				$this->include_filefunctions=1;

				$tmpImage =
$_FILES['tx_YOUR_EXTENSION_pi1']['name']['image'][$n];

				$tmpImage = str_replace  ( ' '  , '_'  ,
$tmpImage );
				$tmpImage = str_replace  ( '	'  , '__'  ,
$tmpImage );
				$tmpImage =
$this->fileFunc->cleanFileName($tmpImage,$this->charset);
				#echo $tmpImage.'<br>';

				// getting unique filename
				$destination =
$this->fileFunc->getUniqueName($tmpImage, $this->uploadFolder,
$dontCheckForUnique=0);

				// move file to file-directory
				// should return a value, but it doesn't (at
least with windows)
				$uploaded =
t3lib_div::upload_copy_move($tmpName,$destination);

				// Extract filename from path
				$imgArray = Array($destination);
				$tmpImages =
t3lib_div::removePrefixPathFromList($imgArray,$this->uploadFolder);
				$tmpImage = $tmpImages[0];
			}

			// commas are needed to find position of images when
some fields keep empty
			// i.e. the first two images have landscape-format,
the rest has portrait-format
			if ($tmpImage) {
				if ($n <= $this->allowedLandscapeAmount) {
					$newData['images'][0] .= ($n <
$this->allowedLandscapeAmount) ? $tmpImage.',' : $tmpImage;
				}
				else {
					$newData['images'][1] .= ($n <
$this->maxImgAmount) ? $tmpImage.',' : $tmpImage;
				}
			}
			else {
				if ($n <= $this->allowedLandscapeAmount) {
					$newData['images'][0] .= ($n <
$this->allowedLandscapeAmount) ? ',' : '';
				}
				else {
					$newData['images'][1] .= ($n <
$this->maxImgAmount) ? ',' : '';
				}
				#$newData['image'] .= ($n <
$this->maxImgAmount) ? ',' : '';
			}
			#$emptyString[1] .= ($n < $this->maxImgAmount) ? ','
: '';
			if ($n < $this->allowedLandscapeAmount) {
				$emptyString[0] .= ',';
			}
			elseif ($n < $this->maxImgAmount) {
				$emptyString[1] .= ',';
			}
		}

		#$saveData['image'] = ($newData['image'] != $emptyString) ?
$newData['image'] : '';
		$saveData['img_landscape'] = ($newData['image'][0] !=
$emptyString[0]) ? $newData['image'][0] : '';
		$saveData['img_portrait'] = ($newData['image'][1] !=
$emptyString[1]) ? $newData['image'][1] : '';
		#t3lib_div::debug($saveData);
________________________

-----Ursprüngliche Nachricht-----
Von: typo3-german-bounces at lists.netfielders.de
[mailto:typo3-german-bounces at lists.netfielders.de] Im Auftrag von Rene?
Kockisch
Gesendet: Samstag, 10. Januar 2009 17:02
An: typo3-german at lists.netfielders.de
Betreff: Re: [TYPO3-german] File upload in Extension

hallo david,

hab ich auch schon probiert hat aber leider nicht funktioniert. Hast du 
noch ne andere Idee woran es liegen könnte? Wie gesagt wenn ich es mit 
print_r anzeigen lasse, dann ist alles in dem Array gefüllt. Wie name, 
größe typ und soweiter, error steht auch auf 0 also sollte es ja 
geklappt haben, aber ich habe keine ahnung woran es scheitert.


Gruß
René
_______________________________________________
TYPO3-german mailing list
TYPO3-german at lists.netfielders.de
http://lists.netfielders.de/cgi-bin/mailman/listinfo/typo3-german



More information about the TYPO3-german mailing list