Whole website crashes when I upload a file - php

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

Related

Empty array while uploading .3gp file in php

In my application i have file upload features where users can upload image and video files. I am able to upload all the files except .3gp. When i print in my back end i am getting empty array. File size which i am uploading is 10 MB and max upload in my php.ini is 20 MB. Below is my sample code which is working fine for other file extension.
Front End
<form method="post" name="addAssets" action="/examengine/assets/addassets" enctype="multipart/form-data">
<input type="file" name="upload" class="custom-file-input" id="customFile">
<button type="submit" class="btn pri-btn">Upload</button>
</form>
Backend
function addassets(){
var_dump($_POST);
var_dump($_FILES);
}
Problem Resolved
I changed post_max_size and resolved.

Empty FILES array for large (ish) file uploads

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();.

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.

larger file upload problem with php

I need to upload a csv file to a server. works fine for smaller files but when the file is 3-6 meg its not working.
$allowedExtensions = array("csv");
foreach ($_FILES as $file) {
if ($file['tmp_name'] > '') {
if (!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) {
die($file['name'].' is an invalid file type!<br/>'. ''. '<&lt Go Back');
}
if (move_uploaded_file($file['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo "File has been uploaded";
}
//upload form
<form name="upload" enctype="multipart/form-data" action="<? echo $_SERVER['php_self'];?>?action=upload_process" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="31457280" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
I have also added this to htaccess
php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 200
php_value max_input_time 200
Where am i going wrong?
What is the value of $_FILES['userfile']['error']? Have a look here:
http://php.net/manual/en/features.file-upload.errors.php
Also, whats with this:
if ($file['tmp_name'] > '') {
I don't think that's very healthy.
Just some suggestions to your code
($file['tmp_name'] > '') should be something like ( ! empty($file['tmp_name']))
echoing "possible file upload attack" won't help anyone. If you believe it is a possible attack, log it to a file.
The form action attribute, the $_SERVER['php_self'] should be capitalised because it is a constant, i.e. $_SERVER['PHP_SELF'].
Check your apache error logs and the error should be there?
Check your php.ini file and see if upload_max_filesize in there is set higher than 3 MB, I don't know if the .htaccess has precedence over the php.ini.
The default upload limit on debian php5 is 2MB if I recall correctly, but I am not sure what system you are running on.
You can also check the php values if you create a file e.g. info.php and put it in the same directory as your "problem php script".
The file content should look like this
<?php
phpinfo();
?>
This will give you all php relevant values referring to the directory you are in, hope it helps.

Categories