larger file upload problem with php - 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.

Related

Can't upload audio file with PHP5.5.12 on WampServer 2.5

I have a problem with upload mode in PHP. Can you help me ?
My form in HTML:
<form action="upload_file.php" enctype="multipart/form-data" method="post">
<input type="file" name="file">
<input type="submit" value="ok">
</form>
My PHP code in 'upload_file.php':
<?php
if (!isset($_FILES['file'])){
echo "Failed";
}
else{
echo "All Ok";
}
?>
When I select image, text, zip, rar ... It report "All Ok". But, If I select audio or video, it report "Failed". So I can't upload audio file. What is wrong ? Thank you very much !.
Make sure that the size of your file does not exceed the limitations listed here (your php.ini file)
# The values are just for example!
; Maximum allowed size for uploaded files.
upload_max_filesize = 3M
; Must be greater than or equal to upload_max_filesize
post_max_size = 3M

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

PHP file upload working for .txt but not for .pdf

Below Code is working fine for a .TXT file, but when I am selecting any .PDF file, it does not copies it to the destination path, Could someone please suggest me what should be the problem.
THE HTML code and PHP code as follows:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
<?php
$uploaddir = 'C:\xampp\htdocs\upload\\';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo $uploadfile;
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
Error number 2, given by the $_FILES array, represents UPLOAD_ERR_FORM_SIZE. From the PHP manual:
The uploaded file exceeds the MAX_FILE_SIZE directive that was
specified in the HTML form.
In your HTML you have a hidden form field named MAX_FILE_SIZE. The field value represents the maximum file size of the uploaded file in bytes.
Either adjust the value of the field or remove it. I believe the field was a try from the PHP developers to implement client side browser check for the file size, stopping users from uploading too big files. However to date there is still no browser that enforces the value of that field.
How big is your PDF file?
You may be reaching your web servers in-built MAX_FILE size (which I know you can change if using Apache).
Check your web server configuration.

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.

Categories