Skip to content

Commit

Permalink
Added headers reading support for PHP-CGI (Thanks to Minha)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanparati committed Oct 22, 2011
1 parent bbf9d36 commit 0c84fd3
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions Source/mooupload.php
Expand Up @@ -100,26 +100,26 @@ public static function HTML5_upload($destpath, $send_response = TRUE)
if (!file_exists($destpath))
throw new Exception('Path do not exist!');

$response = array();

// Read headers
$headers = getallheaders();
$protocol = $_SERVER['SERVER_PROTOCOL'];


$max_upload = self::_convert_size(ini_get('upload_max_filesize'));
$max_post = self::_convert_size(ini_get('post_max_size'));
$memory_limit = self::_convert_size(ini_get('memory_limit'));

$limit = min($max_upload, $max_post, $memory_limit);

$response['id'] = $headers['X-File-Id'];
// Read headers
$response = array();
$headers = self::_read_headers();

$response['id'] = $headers['X-File-Id'];
$response['name'] = basename($headers['X-File-Name']); // Basename for security issues
$response['size'] = $headers['Content-Length'];
$response['error'] = UPLOAD_ERR_OK;
$response['finish'] = FALSE;

// Detect upload errors
if ($headers['Content-Length'] > $limit)
if ($response['size'] > $limit)
$response['error'] = UPLOAD_ERR_INI_SIZE;

// Firefox 4 sometimes sends a empty packet as last packet
Expand All @@ -144,7 +144,12 @@ public static function HTML5_upload($destpath, $send_response = TRUE)
else
{
if (filesize($destpath.$filename) == $headers['X-File-Size'])
{
$response['finish'] = TRUE;

/* If uploaded file is finished, maybe you are interested in saving, registering or moving the file */
// my_save_file($destpath.$filename, $response['name']);
}
}


Expand Down Expand Up @@ -216,7 +221,36 @@ public static function _normalize_path($path)
$path .= DIRECTORY_SEPARATOR;

return $path;
}
}

/**
*
* Read and normalize headers
*
* @return array
*
*/
public static function _read_headers()
{

// GetAllHeaders doesn't work with PHP-CGI
if (function_exists('getallheaders'))
{
$headers = getallheaders();
}
else
{
$headers = array();
$headers['Content-Length'] = $_SERVER['CONTENT_LENGTH'];
$headers['X-File-Id'] = $_SERVER['HTTP_X_FILE_ID'];
$headers['X-File-Name'] = $_SERVER['HTTP_X_FILE_NAME'];
$headers['X-File-Resume'] = $_SERVER['HTTP_X_FILE_RESUME'];
$headers['X-File-Size'] = $_SERVER['HTTP_X_FILE_SIZE'];
}

return $headers;

}

}

Expand Down

0 comments on commit 0c84fd3

Please sign in to comment.