Empty FILES array for large (ish) file uploads - php

I've got some legacy code that for the life of me I cant see why it's suddenly stopped working.
Very basic upload script:
if($_FILES['csvfile']['name']){
//if no errors...
if(!$_FILES['csvfile']['error']){
//now is the time to modify the future fle name and validate the file
$new_file_name = strtolower($_FILES['csvfile']['tmp_name']); //rename file
//move it to where we want it to be
move_uploaded_file($_FILES['csvfile']['tmp_name'], 'active_leads1.csv');
//echo 'Congratulations! Your file was accepted.';
}else{
//set that to be the returned message
//echo 'Ooops! Your upload triggered the following error: '.$_FILES['csvfile']['error'];
die("Unfortuanatly there was an error: {$_FILES['csvfile']['error']}");
}
}else{
die("Unfortuanatly there was an error: ".print_r($_FILES,true)."");
}
And equally the form is just as basic:
<form action="csvconvert.php" method="post" enctype="multipart/form-data">
<input type="file" name="csvfile" size="25" />
<input type="submit" name="submit" value="Submit" />
</form>
And yet, it fails on a file c. 11MB. Smaller files are ok.
Checked the post limit and upload limit, both are fine (256M and 128M), max input time is 240, and yet if I print_r $_POST and $_FILES they are both empty.
Has anyone come across this before? Any help would be appreciated!

If smaller files are ok, are you sure your upload_max_filesize is larger than 11MB? What is the value of upload_max_filesize when you use echo phpinfo();.

Related

Server freezes when uploading large sized images in php

I have simple html form with post method, which sends an image to my action.php file:
<form action="action.php" method="post" enctype="multipart/form-data">
<input type="text" name="name"/>
<input type="file" name="avatar"/>
<input type="submit" name="submit" value="Create"/>
</form>
And my action.php file looks like this:
if(isset($_POST['name'])) {
$error = $_FILES['avatar']['error'];
echo $error;
}
It works fine with small sized images, but when I try to submit images with approximately 2mb size the server just stops responding. It even doesn't send post request to my action.php.
Note: I've already set my upload_max_filesize = 64M, but the same thing is happening. It doesn't show any errors
Change the value of post_max_size = 70M.
Must be greater than or equal to upload_max_filesize.
After modifying php.ini file(s), you need to restart your HTTP server to use new configuration.
If problem persist you could try these solutions (1,2,3).

Whole website crashes when I upload a file

When I upload a file using an HTML form, as soon as I hit the "upload" button, the whole site crashes. I can't see the website for about 40 minutes but the FTP server is still available.
I don't understand why this is happening. Yesterday it worked fine and today it doesn't. Nothing was changed.
I use a simple form:
<form enctype="multipart/form-data" action="csvupload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Upload File: <input name="csv" type="file" />
<input type="submit" value="Send File" />
</form>
how's your php error catching for the file upload?
I find code like this can be informative in these crash situations.
$file=$_FILES['file'];
if (isset($_FILES['file'])==true){
if ($file["error"] > 0)
{
$errors_array = array( 0=>"There is no error, the file uploaded with success",
1=>"The uploaded file exceeds the upload_max_filesize directive!",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
5=>"Unknown Error",
6=>"Missing temporary folder"
);
$error=$file["error"];
}
}
keeps the move_uploaded_file from being executed.
I've found the problem...as i worked on my code, i made a copy & past failure so a for loop was in a infinite loop. facepalm

Why doesn't $_POST recognise certain files uploaded?

You would not believe this fault, I have no idea what's going on. When I try to upload files, some of them refuse to upload.
<html>
<body>
<?php
var_dump($_POST);
if ($_POST['fileadd']){
echo "Type: " . $_FILES["file"]["type"] . "<br />";
print_r($_POST); echo "<br>"; print_r($_FILES);
}
?>
<form action="" method="POST"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" class="bigtext" style="width: 80%;">
<br>
<input type="submit" class="bigbutton" id="showloader" name="fileadd" value="Upload the song" />
</form>
</body>
</html>
Here it is in action.
The problem is when it says array 0 which it shouldn't say.
Also, how come when I choose certain files, the $_POST array is nil/zero, when it should at least have fileadd in it, as the name of the submit button?
After seeing full code and conversing with the OP on finding a solution to the problem at hand, have concluded the problem to be the following points:
This:
if ($_POST['fileadd']){
Needed to be changed to:
if(isset($_POST['fileadd']))
in order to check if the Submit button has been set.
Also the upload max size was set too low in accordance with the size of files attempted to be uploaded, using the following in .htaccess to increase it.
php_value upload_max_filesize 48M
php_value post_max_size 48M
You're simply assuming that your code is perfect. It's not. It assumes that file uploads ALWAYS succeed, and doesn't allow for the possibility of failure. You need to have something more like:
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
die("File upload failed with error code #" . $_FILES['file']['error']);
}
The error codes are defined here: http://www.php.net/manual/en/features.file-upload.errors.php

bool(false) Uploading a file

I have had problems with a simple php script in which I can upload a file to a certain folder. I have tried multiple ways in doing this and I still have not had success.
Any errors in my code or advice on how to correct the issue will be taken gracefully.
Main Php Code:
<p>Browse For a File on your computer to upload it!</p>
<form enctype="multipart/form-data" action="upload_photos.php" method="POST">
Choose Photo:
<input name="userfile" type="file" /><br />
<input type="submit" value="Upload Photo" />
<?PHP
if ($userfile_size>250000)
{$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.<BR>";
$file_upload="false";}
else{
if (!($userfile_type<>"image/jpeg" OR $userfile_type<>"image/tiff" OR $userfile_type<>"image/png"))
{$msg=$msg."Your uploaded file must be of JPG, PNG, or tiff. Other file types are not allowed<BR>";
$file_upload="false";}
}
?>
</form>
</label>
</form>
Php code that is called upon on click (upload_photos.php)
<?php
$target_path="uploads/";
chmod("uploads/", 0755);
$target_path=$target_path . basename( $_FILES['uploadedfile']['name']);
$test=move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
if($test) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
var_dump($test);
}
?>
I do not understand why my end results [upon clicking "Upload Files" Button] include only the following results:
"There was an error uploading the file, please try again!bool(false)"
One more thing: I have also tried using the full computer folder path for $target_path and chmod.
Does anybody see what I am doing wrong?
You have <input name="userfile" but then use $_FILES['uploadedfile'] in your script - use one or the other.
Other than that, make sure the chmod worked and the folder is writable.
bool(false) is the output of var_dump($test);, indicating that move_uploaded_file is returning false.
As a basic debugging step, you should try var_dump($_FILES) to make sure you're accessing the right element of that array (I can tell from your code that you aren't, the index will be the name attribute of your <input type="file"/> element).
You have at least one other serious flaw in your logic... The PHP code in your upload form doesn't make any sense. That block of PHP code will execute server-side before the user has ever uploaded a file. It can't possibly work. The two variables you're checking, $userfile_size and $userfile_type, are not defined anywhere.
In my case, I forgot to create a folder where I want to upload. So check once the specified upload path is available or not.

Upload php script problems

Using Expression Web, want to upload images to my web pages, not getting error messages even though written in script. Set the upload folder to share with everyone. php.ini is set to file_uploads=on;upload_max_size=128M. On firefox the script times out and on IE "Internet Explorer can not display page."
I have been searching for answers for weeks. Every other script I have tried works even though I am a novice at php. This upload thing has gotten me scratching my head. Most recent upload script attempt:
if (isset($_POST['submitted'])) {
// Check for an uploaded file:
if (isset($_FILES['upload'])) {
// Validate the type. Should be JPEG or PNG.
$allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
if (in_array($_FILES['upload']['type'], $allowed)) {
$target_path = "C:/uploads/";
$name=$_FILES['upload']['name'];
$error=$_FILES['upload']['error'];
$tmp_name=$_FILES['upload']['tmp_name'];
if($error==UPLOAD_ERR_OK){
if($_FILES['upload']['size']>0){
move_uploaded_file($tmp_name, $target_path.$name);
print ("The file ".$name." has been uploaded.\n");
} else{
print ("There was an error uploading the file, please try again!\n");
}
}elseif ($error==UPLOAD_ERR_NO_FILE) {
print("No files specified.\n");
}else{
print("Upload failed.\n");
}
print("\n");
}//End of !in_array IF.
} // End of isset($_FILES['upload']) IF.
} // End of the submitted conditional.
?>
<div>
<form action="photogallery.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288" />
<p><b>File:</b><input name="upload" type="file" /></p>
<input name="submit" type="submit" value="Submit" />
<input name="submitted" type="hidden" value="TRUE" />
</form>
</div>
I am trying to upload images into upload folder in my directory and then display on my web page. I have put images into the folder manually and they will show on my webpage via a link but it wont work using these scripts. My site is not live it is in developement and I am using XAMPP and Microsoft development Server.
The actual error is hard to tell. But there are a few points to mention on your script (typical upload script problems):
The ['name'] constanct could be empty, which would explain an upload error.
But it could also be tampered with, and contain things like "../Windows/System.ini", which wouldn't be so good for your script since you use it unfiltered as output filename.
The MIME ['type'] is likewise unreliable. But that's likely not the problem here. At the very least use strtolower() on it instead of listing multiple MIME alternatives.
Lastly enable error_reporting(E_ALL); which will tell you if move_upload_file() permission issues are the problem.
I would suggest that you start with a simple
move_uploaded_file($_FILES["upload"]["tmp_name"], "C:/uploads/test.1");
And then gradually extend it with your other conditions to find the cause of your upload issue.

Categories