Project

General

Profile

Bug #75395 » mittwaldTYPO3FileUploadTest.php

Ralf Zimmermann, 2016-06-15 17:38

 
<?php

define('TYPO3_OS', getTypo3Os());
define('PATH_thisScript', getPathThisScript());
define('PATH_site', getPathSite(''));

$scriptFileName = dirname($_SERVER['SCRIPT_FILENAME']);
$fileInfo = new \SplFileInfo($scriptFileName);
$tmpName = $_FILES['tx_form_form']['tmp_name']['tx_form']['fileupload'];

echo "\$_SERVER['SCRIPT_FILENAME']: " . $_SERVER['SCRIPT_FILENAME'] . "<br />";
echo "\$_SERVER['SCRIPT_FILENAME']->getPath(): " . $fileInfo->getPath() . "<br />";
echo "\$_SERVER['SCRIPT_FILENAME']->getRealPath(): " . $fileInfo->getRealPath() . "<br />";
echo "getcwd(): " . getcwd() . "<br />";
echo "PATH_site: " . PATH_site . "<br />";

if ($tmpName) {
echo "upload file tmp name: " . $tmpName . "<br />";
echo "filename after moving to TYPO3 temp: " . upload_to_tempfile($tmpName) . "<br />";
}

/**
* Methods from TYPO3 CMS Core
*/

/**
* Will move an uploaded file (normally in "/tmp/xxxxx") to a temporary filename in PATH_site."typo3temp/" from where TYPO3 can use it.
* Use this function to move uploaded files to where you can work on them.
* REMEMBER to use \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile() afterwards - otherwise temp-files will build up! They are NOT automatically deleted in PATH_site."typo3temp/"!
*
* @param string $uploadedFileName The temporary uploaded filename, eg. $_FILES['[upload field name here]']['tmp_name']
* @return string If a new file was successfully created, return its filename, otherwise blank string.
* @see unlink_tempfile(), upload_copy_move()
*/
function upload_to_tempfile($uploadedFileName)
{
if (is_uploaded_file($uploadedFileName)) {
$tempFile = _tempnam('upload_temp_');
move_uploaded_file($uploadedFileName, $tempFile);
return @is_file($tempFile) ? $tempFile : '';
}
}

/**
* Create temporary filename (Create file with unique file name)
* This function should be used for getting temporary file names - will make your applications safe for open_basedir = on
* REMEMBER to delete the temporary files after use! This is done by \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile()
*
* @param string $filePrefix Prefix for temporary file
* @param string $fileSuffix Suffix for temporary file, for example a special file extension
* @return string result from PHP function tempnam() with PATH_site . 'typo3temp/' set for temp path.
* @see unlink_tempfile(), upload_to_tempfile()
*/
function _tempnam($filePrefix, $fileSuffix = '')
{
$temporaryPath = PATH_site . 'typo3temp/';

if ($fileSuffix === '') {
$tempFileName = fixWindowsFilePath(tempnam($temporaryPath, $filePrefix));
echo "GeneralUtility::tempnam after PHP tempnam(): <pre>" . $tempFileName . "</pre><br />";
} else {
do {
$tempFileName = $temporaryPath . $filePrefix . mt_rand(1, PHP_INT_MAX) . $fileSuffix;
} while (file_exists($tempFileName));
touch($tempFileName);
clearstatcache(null, $tempFileName);
}
return $tempFileName;
}

/**
* Fixes a path for windows-backslashes and reduces double-slashes to single slashes
*
* @param string $theFile File path to process
* @return string
*/
function fixWindowsFilePath($theFile)
{
return str_replace(array('\\', '//'), '/', $theFile);
}

/**
* Calculate the document root part to the instance from PATH_thisScript
*
* We have two main scenarios for entry points:
* - Directly called documentRoot/index.php (-> FE call or eiD include): index.php sets $relativePathPart to
* empty string to hint this code that the document root is identical to the directory the script is located at.
* - An indirect include of any Backend related script (-> typo3/index.php or the install tool).
* - A Backend script: This is the case for the index.php dispatcher and other entry scripts like 'cli_dispatch.phpsh'
* or 'typo3/index.php' that are located inside typo3/ directly. In this case the Bootstrap->run() command sets
* 'typo3/' as $relativePathPart as base to calculate the document root.
*
* @param string $relativePathPart Relative directory part from document root to script path
* @return string Absolute path to document root of installation
*/
function getPathSite($relativePathPart)
{
$entryScriptDirectory = getUnifiedDirectoryNameWithTrailingSlash(PATH_thisScript);
if ($relativePathPart !== '') {
$pathSite = substr($entryScriptDirectory, 0, -strlen($relativePathPart));
} else {
$pathSite = $entryScriptDirectory;
}
return $pathSite;
}

/**
* Calculate PATH_thisScript
*
* First step in path calculation: Goal is to find the absolute path of the entry script
* that was called without resolving any links. This is important since the TYPO3 entry
* points are often linked to a central core location, so we can not use the php magic
* __FILE__ here, but resolve the called script path from given server environments.
*
* This path is important to calculate the document root (PATH_site). The strategy is to
* find out the script name that was called in the first place and to subtract the local
* part from it to find the document root.
*
* @return string Absolute path to entry script
*/
function getPathThisScript()
{
$supportedCgiServerApis = array(
'fpm-fcgi',
'cgi',
'isapi',
'cgi-fcgi',
'srv', // HHVM with fastcgi
);

$cgiPath = '';
if (isset($_SERVER['ORIG_PATH_TRANSLATED'])) {
$cgiPath = $_SERVER['ORIG_PATH_TRANSLATED'];
} elseif (isset($_SERVER['PATH_TRANSLATED'])) {
$cgiPath = $_SERVER['PATH_TRANSLATED'];
}
if ($cgiPath && in_array(PHP_SAPI, $supportedCgiServerApis, true)) {
$scriptPath = $cgiPath;
} else {
if (isset($_SERVER['ORIG_SCRIPT_FILENAME'])) {
$scriptPath = $_SERVER['ORIG_SCRIPT_FILENAME'];
} else {
$scriptPath = $_SERVER['SCRIPT_FILENAME'];
}
}
// Replace \ to / for Windows
$scriptPath = str_replace('\\', '/', $scriptPath);
// Replace double // to /
$scriptPath = str_replace('//', '/', $scriptPath);
return $scriptPath;
}

/**
* Remove file name from script path and unify for Windows and Unix
*
* @param string $absolutePath Absolute path to script
* @return string Directory name of script file location, unified for Windows and Unix
*/
function getUnifiedDirectoryNameWithTrailingSlash($absolutePath)
{
$directory = dirname($absolutePath);
if (TYPO3_OS === 'WIN') {
$directory = str_replace('\\', '/', $directory);
}
return $directory . '/';
}

/**
* Determine the operating system TYPO3 is running on.
*
* @return string Either 'WIN' if running on Windows, else empty string
*/
function getTypo3Os()
{
$typoOs = '';
if (!stristr(PHP_OS, 'darwin') && !stristr(PHP_OS, 'cygwin') && stristr(PHP_OS, 'win')) {
$typoOs = 'WIN';
}
return $typoOs;
}

?>

<form action="/test.php" id="field-1" name="id-1" method="post" enctype="multipart/form-data">
<ol>
<li class="csc-form-2 csc-form-element csc-form-element-textline">
<label for="field-2">
Textfield
</label>
<input type="text" name="tx_form_form[tx_form][textfield]" id="field-2">
</li>
<li class="csc-form-3 csc-form-element csc-form-element-fileupload">
<label for="field-3">
Fileupload
</label>
<input type="file" name="tx_form_form[tx_form][fileupload]" id="field-3">
</li>
<li class="csc-form-4 csc-form-element csc-form-element-submit">
<label for="field-4">
</label>
<input type="submit" name="tx_form_form[tx_form][4]" value="Send" id="field-4">
</li>
</ol>
</form>
    (1-1/1)