Hello there,
I was just wondering if anybody could be of assistance.
I have a very strange problem which is apache related I assume… maybe. I have a pretty simple PHP file upload script. It uploads files to my Linux server without any issues as I have all possible and impossible PHP errors’ logging turned on. It reports no errors and files upload well. The only problem is the files double in size during the upload process. For example if the original file is 10Kb before the upload then it becomes 20Kb after the upload. Then if I want to try to view the file via apache (any web browsers) or download it back it says the file is corrupt and it is impossible to open it. I tested the script extensively and it worked OK on 3 different servers but does not seem to work on mine… There are no errors in any logs reported anywhere at all… The only problem I accidentally found manually myself was this mysterious double in file size during the upload… I am getting completely lost and would appreciate any help / suggestions at all. Many thanks in advance!
I tried adding / removing these of course as per known old bugs but no joy:
<Files *.php>
AddInputFilter PHP .php
AddOutputFilter PHP .php
<Files .php>
SetOutputFilter PHP
SetInputFilter PHP
AddType application/x-httpd-php .php4 .php3 .phtml .php
AddType application/x-httpd-php .php .phtml .phtm .inc .oc
The script is copy-pasted down-below:
$allowed_filetypes = array(’.jpg’,’.gif’,’.png’,’.doc’,’.docx’,’.xls’);
$max_filesize = 524288;
$filename = $_FILES[‘uploadedfile’][‘name’];
$ext = substr($filename, strpos($filename,’.’), strlen($filename)-1); // Get the filename’s extension.
// Check if $filename is empty and no files for uploading were selected.
if (empty($filename)) {
echo “There weren’t any files selected for uploading.
Please, press the back button.”;
exit();
}
// Check if the filetype is allowed.
if(!in_array($ext,$allowed_filetypes))
die(‘The file you attempted to upload was not in allowed extension.
Please, press the button and then reset.’);
// Now check the filesize, if it is too large.
if(filesize($_FILES[‘uploadedfile’][‘tmp_name’]) > $max_filesize)
die(‘The file you attempted to upload is too large.
Please, press the back button, re-size, and re-upload your file.’);
if (file_exists(‘uploads/’ . $_FILES[‘uploadedfile’][‘name’]))
{
echo “” . $_FILES[‘uploadedfile’][‘name’] . " already exists.
Please, press the back button, re-name, and re-upload your file.";
exit();
}
if (is_uploaded_file($_FILES[‘uploadedfile’][‘tmp_name’])) {
echo “File “. $_FILES[‘uploadedfile’][‘name’] .” uploaded successfully.\n”;
} else {
echo "Possible file upload attack: ";
echo “filename '”. $_FILES[‘upoadedfile’][‘tmp_name’] . “’.”;
}
if(is_uploaded_file($_FILES[‘uploadedfile’][‘tmp_name’]))
{
echo ("$filename is uploaded via HTTP POST. “);
}
else
{
echo (”$filename is not uploaded via HTTP POST. ");
}
$target_path = “uploads/”;
$target_path = $target_path . basename( $_FILES[‘uploadedfile’][‘name’]);
if(move_uploaded_file($_FILES[‘uploadedfile’][‘tmp_name’], $target_path)) {
echo "The file “. basename( $_FILES[‘uploadedfile’][‘name’]).
" has been uploaded.”;
echo ’ Here is some more debugging info:’;
print_r($_FILES);
exit();
}else{
echo “There was an error uploading the file, please try again!”;
}
echo ’ Here is some more debugging info:’;
print_r($_FILES);
// Yes. There are 3 different additional checks to see if the file was uploaded.
Here is also what the print_r($_FILES) normally says,
File test.doc uploaded successfully. test.doc is uploaded via HTTP POST. The file test.doc has been uploaded. Here is some more debugging info:Array ( [uploadedfile] => Array ( [name] => Master.doc [type] => application/msword [tmp_name] => /uploads/php3t4UJX [error] => 0 [size] => 83376 ) )
The uploaded file’s size in this case is 83376 when the original was 41688 (doubled the size during the process of uploading).
My server’s upload_max_filesize is set to 2 MB. The upload_temp_dir is specified (otherwise the PHP would give the error number two and no files would upload). file_uploads is ON. All the other imaginable settings I am aware of are also set correctly… I guess…
I have spent 3 days trying to troubleshoot it but without any success… Would be really grateful for any suggestions / help at all.
The only extra idea that has just crossed my mind while writing this post is to additionally declare the file size before the uploading and pass it back and forth so that the original file’s size would stay the same after the uploading… Not sure if this is the right thing to do though but cannot really figure out where on the server it has to be set. I am also starting to think that this is an apache problem. I am still on 2.2.22
In fact, would also upgrading to the very latest PHP and Apache versions help?