There are several factors that must be considered when uploading files with PHP. I suggest that you go back and reexamine the relevant manual page:
http://us3.php.net/manual/en/features.file-upload.php
1.) You didn't include the hidden form element MAX_FILE_SIZE.
2.) You didn't explicitly specify an upload_max_filesize in php.ini.
There are two php.ini directives with which you should concern yourself, in addition to those noted in your original post:
post_max_size
upload_max_filesize
Try the following:
File upload class example (save as upload.class.php):
<?php
class Upload
{
//Handles a file upload using PHP's built-in functions.
//Returns an array that contains a boolean value (that represents success or failure)
//PHP's error code (which is 0 if no error), and an error message (if the error code is not 0) or the moved file's
//new location and name (if the error code is 0).
function processUpload($tmpFileName)
{
//First, we have to make sure that the $_FILES superglobal is actually populated.
if (count($_FILES) > 0) {
//Next, we'll check to see if PHP reported any problems with the file.
//Let's capture the error code (even if it's 0), so we can
//include the error code in the function's return value.
$returnArray['errcode'] = $_FILES['uploadedFile']['error'];
//If the "error" key in the $_FILES superglobal equals 0, PHP didn't detect any problems
//with the upload, and we can skip ahead. If not, we need to do some more work.
if ($_FILES['uploadedFile']['error'] != 0) {
switch ($_FILES['uploadedFile']['error']) {
//First, let's address issues over which the user has no control.
//(These messages were taken directly from the PHP Manual.)
case 1:
$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
break;
case 2:
$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
break;
case 6:
$error = 'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.';
break;
case 7:
$error = 'Failed to write file to disk. Introduced in PHP 5.1.0.';
break;
case 8:
$error = 'File upload stopped by extension. Introduced in PHP 5.2.0.';
break;
//And now for problems the user is able to help assauge.
case 3:
$error = 'The uploaded file was only partially uploaded.';
break;
case 4:
$error = 'No file was uploaded.';
break;
}
if (isset($error)) {
$returnArray['result'] = FALSE;
$returnArray['message'] = $error;
}
else {
$returnArray['result'] = TRUE;
}
}
else {
$returnArray['result'] = TRUE;
}
//If none of the above errors prevented the upload
//from at least landing in PHP's tmp (temporary) directory,
//let's see what the file's name is.
if (isset($_FILES['uploadedFile']['tmp_name']) && $_FILES['uploadedFile']['tmp_name'] != '') {
$returnArray['tmpName'] = $_FILES['uploadedFile']['tmp_name'];
}
}
else {
$returnArray['result'] = FALSE;
$returnArray['message'] = 'PHP\'s $_FILES superglobal is not populated for some reason. The upload cannot be processed.';
}
return $returnArray;
}
//Moves a file (provided a fully-qualified path and file name) from
//its temporary location (after a PHP-handled POST/PUT file upload)
//to a more permanent location, e.g., on a Web server's hard disk drive.
function moveUploadedFile($tmpFile, $newDestination)
{
if (@file_exists($tmpFile)) {
$res = move_uploaded_file($tmpFile, $newDestination);
if ($res !== FALSE) {
return TRUE;
}
else {
return FALSE;
}
}
else {
return FALSE;
}
}
}
PHP business logic and HTML form:
<?php
//Require the necessary upload class file.
require('upload.class.php');
//We only want to do this stuff if a file has been uploaded via the POST method.
if (count($_POST) > 0) {
//Attempt to set the php.ini value that governs how long
//"temporary" data is allowed to exist before PHP's "garbage collection"
//process destroys it.
//
//The default value is only 24 minutes. We may need more like an hour here.
@ini_set("session.gc_maxlifetime","3600");
//Let's attempt to validate the file upload.
$res = Upload::processUpload('uploadedFile');
if ($res['result'] === TRUE) {
//The file made it into PHP's temporary directory without issues. Nice.
//Next, let's try to move the file to another location, so PHP doesn't
//automatically delete the file at the end of script execution.
$sourceFile = $res['tmpName'];
$targetFile = '/some/location/on/your/filesystem/file.txt';
$res = Upload::moveUploadedFile($sourceFile, $targetFile);
if ($res === TRUE) {
//The file was uploaded AND moved successfully.
//That's it!
}
else {
die('The file was uploaded successfully, but could not be moved out of PHP\'s temporary directory.');
}
}
else {
die('The file upload could not be processed because: ' . $res['message']);
}
}
else {
die('No file was uploaded! Did you choose a file before clicking "Upload File"?');
}
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<!-- MAX_FILE_SIZE must precede the file input field. -->
<input type="hidden" name="MAX_FILE_SIZE" value="650000000" />
<!-- Name of input element determines name in PHP's $_FILES array. -->
<input name="uploadedFile" type="file" />
<input type="submit" value="Upload File" />
</form>
Let us know if you are still stuck.