Page 1 of 1

Can't upload files even though settings are correct

Posted: Tue Nov 13, 2007 3:29 pm
by iandunn
I've got a form to upload a file, but if I try to upload anything larger than 2MB it fails with $_FILES['userfile']['error'] being set to 1, which means the file was larger than the limit set in upload_max_filesize. My upload_max_filesize is set to 10M, though, as well as post_max_size and the form's max_file_size. The source is below and you can try it for yourself here. The values in the grey box @ the bottom are being pulled from ini_get(), so that shows that the values are set correctly. I've done a lot of searching but can't find any reason why it would say that the limit is 10MB, but that a 2MB file is over the limit.

Code: Select all

<html>
<head>
	<title>PHP upload test</title>
	
	<style>
		
		.errorBox
		{
			border: 1px solid black;
			padding: 5px 5px 5px 5px;
			width: 300px;
			margin-bottom: 30px;
			color: red;
		}
		
		.infoBox
		{
			margin-top: 50px;
			background-color: #C0C0C0;
			width: 200px;
			padding: 5px 5px 5px 5px;
			border: 1px solid black;
		}
		
	</style>
</head>
<body>

<?php

	$uploadErrorCodes = array(
		0 => "There is no error, the file uploaded with success",
		1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
		2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
		3 => "The uploaded file was only partially uploaded",
		4 => "No file was uploaded",
		6 => "Missing a temporary folder",
		7 => "Failed to write file to disk.",
		8 => "File upload stopped by extension"
	);

	// Uncomment this section to force PHP values
	// upload_max_filesize, post_max_size and max_input_time cannot be set with ini_set
	$iniSetErrors = array();
	if(!ini_set('display_errors', '1'))
		array_push($iniSetErrors, 'display_errors');
	if(!ini_set('error_reporting', E_ALL))
		array_push($iniSetErrors, 'error_reporting');
	if(ini_get('memory_limit') != '')
		if(!ini_set('memory_limit', '12M'))
			array_push($iniSetErrors, 'memory_limit');
	if(!ini_set('max_execution_time', '160'))
		array_push($iniSetErrors, 'max_execution_time');
		
	
	if(count($iniSetErrors) > 0)
	{
		echo '<div class="errorBox">The following PHP directives couldn\'t be set:<br />';
		foreach($iniSetErrors as $iniSetError)
			echo $iniSetError . '<br />';
		echo 'Safe mode is <strong>'. ini_get('safe_mode') .'</strong>';
		echo '</div>';
	}
	
	// If file was uploaded, print $_FILES
	if(isset($_POST['upload-submit']))
	{
		$uploadError = $_FILES['userfile']['error'];
		$filesArray = print_r($_FILES, true);
		
		if($uploadError != 0)
			$filesArray = str_replace("[error] => ". $uploadError, "[error] => ". $uploadError . " - ". $uploadErrorCodes[$uploadError], $filesArray);
		
		echo '<p><pre>$_FILES '. $filesArray . '</pre></p>';
	}
	
	// Upload form
	echo '
		<form enctype="multipart/form-data" action="" method="POST">
		    <input type="hidden" name="max_file_size" value="10000000" />
		    Send this file: <input name="userfile" type="file" />
		    <input type="submit" name="upload-submit" value="Send File" />
		</form>
	';
	
	// PHP directives any other relevant information
	$max_file_size = isset($_POST['max_file_size']) ? $_POST['max_file_size'] : '';
	echo sprintf('
		<div class="infoBox">
			file_uploads: %s<br />
			upload_max_filesize: %s<br />
			post_max_size: %s<br />
			memory_limit: %s<br />
			max_input_time: %s<br />
			max_execution_time: %s
			form max_file_size: %s<br />
		</div>
		',
		ini_get('file_uploads'),
		ini_get('upload_max_filesize'),
		ini_get('post_max_size'),
		(ini_get('memory_limit') == '' ? 'N/A' : ini_get('memory_limit')),
		ini_get('max_input_time'),
		ini_get('max_execution_time'),
		($max_file_size == '' ? 'N/A' : $_POST['max_file_size'])
	);
	
?>

</body>
</html>