Force efficient small file upload for IE9/PHP/jQuery - php

I want to have a small upload limit (e.g. 100 kb for testing, perhaps 6 Mb ultimately). The size of the upload can be checked:
before the upload
fail when too much is uploaded
after the upload
If the user is trying a 1 Gb file ideally (1) should happen so that the file isn't uploaded at all. If not, (2) should happen so that it doesn't take long before the user knows the file is too big. I'd like to avoid the other possibility (3).
In HTML5 but not in IE9 the filesize can be checked before uploading using:
this.files[0].size
Get file size before uploading
In IE9 the following might work if the security settings are adjusted:
var objFSO = new ActiveXObject("Scripting.FileSystemObject"); var filePath = $("#" + fileid)[0].value;
var objFile = objFSO.getFile(filePath);
var fileSize = objFile.size; //size in kb
Ideally I'd like to use a method that works with IE9. I've heard about flash files being used. I'd like a method that is separate - not using a big plugin.
Here is my code. At the moment it uploads the whole file before it checks if the file size is too big.
<?php
if (isset($_FILES['myfile'])) {
if ($_FILES['myfile']['error'] == UPLOAD_ERR_FORM_SIZE) {
// $_FILES['myfile']['size'] is 0
echo 'Error: file is too big!<br>';
}
else if ($_FILES['myfile']['size'] > 100000) {
echo 'File size is too big!<br>';
}
else {
echo 'File uploaded ok.<br>';
}
}
var_dump($_FILES);
?>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input type="file" name="myfile" />
<input type="submit" value="Submit" />
</form>
The small upload limit is just for one form so I don't want to change the global PHP settings file.
I'm not sure using MAX_FILE_SIZE is a good idea - the person still has to upload the entire file and the filesize data is lost (I might want to tell people how big their upload was)
ini_get('post_max_size') returns 8M and ini_get('upload_max_filesize') returns 32M. If files that are over 8 Mb try to be uploaded the file is still fully uploaded (even 40+ Mb). After larger than 8 Mb files are uploaded var_dump($_FILES) returns an empty array.
If the file is bigger than about 600 Mb I get a 413 error... that is (1).
So I want to do (1) or (2) in IE9, PHP and jQuery.

Related

Server side file validation

I'm trying to make file upload to server by form:
<form action="send_valid.php" method="POST" enctype= "multipart/form-data">
<br>
<input type="file" name="pdf" id="pdf" accept="application/pdf"/>
<input type="hidden" name="MAX_FILE_SIZE" value="10000000"/>
<input type="submit" value="Wyślij">
</form>
and I want to allow user to send only pdf files of a max size 10Mb.
My php configuration for uploads is:
file_uploads = On
upload_tmp_dir = "E:\Xampp\tmp"
upload_max_filesize = 11M
max_file_uploads = 20
post_max_size = 12M
To check file size I use:
if($_SERVER["REQUEST_METHOD"] == "POST"){
var_dump($_FILES);
if(extract($_FILES)){
if($pdf['size']>10000000){
echo "File size is too large!";
}
}
Now I want to show user an error (for now) with echo when file is too big. It works fine if it is lower than 10Mb (even the code above works when I change size to 1Mb and file is larger then it will display echo), but for files of 10Mb and above it produces that error:
Warning: POST Content-Length of 11450416 bytes exceeds the limit of 8388608 bytes in Unknown on line 0
array(0) { }
I don't have any clue why it shows it exceeds 8Mb since in configs I couldn't find 8Mb anywhere.
Where can be the problem? Is there a way to catch an upload that exceeds configuration setting to not show user the php server error?
And if I want to make file validation does above method and checking file extension with for examle:
$ext = pathinfo($_POST['pdf'], PATHINFO_EXTENSION);
is it enough? Any insight on file validation would be really helpful.
Probably this
ini_set('post_max_size', '512M');
ini_set('upload_max_filesize', '512M');
Change 512 to any of you want.
Update the values of post_max_size and upload_max_filesize in your php configuration file.
Note that the values are measured in bytes.
Reference
http://php.net/manual/en/ini.core.php#ini.post-max-size
http://php.net/manual/en/ini.core.php#ini.upload-max-filesize
Put this code before move_uploaded_file function
if($_FILES['pdf']['size']>10000000) {
exit("File size is too large!");
}
Thanks

PHP - $_FILES empty after large file upload on Azure

I've setup an Angular based file upload using the ng-file-upload plugin (https://github.com/danialfarid/ng-file-upload) and I've been handling the file upload with a PHP script.
The file upload and script work on smaller files (tested it on < 1MB), but fails on a larger file (9MB). This leads me to believe that there's a file upload issue. However, I've already created a .user.ini file in the /wwwroot folder with a single line:
upload_max_filesize=20M
Is there another reason why the $_FILES and $_POST arrays would be empty?
JS Code:
Upload.upload({
url: '/scripts/receiveFile.php',
file: file
}).then(function(resp) {
console.log(resp.data);
}, function(resp) {
console.log(resp.data);
}, function(evt) {
var progressPercent = parseInt(100.0 * evt.loaded / evt.total);
console.log(progressPercent + "%");
});
HTML Code:
<div>
<h1>Upload</h1>
<input type="file" accept=".zip" ngf-select="submitFile($file)"></input>
</div>
PHP Code:
$file_name = basename($_FILES['file']['name']);
$file_tmp_name = $_FILES['file']['tmp_name'];
The script fails because 'file' is undefined in the $_FILES array - because the $_FILES array is empty.
Thanks!
PHP has a confusing was of doing file uploads. To set a larger upload size, you should set both upload_max_filesize and post_max_size. These can be independently different values, as they do different things.
post_max_size is the maximum file size that can be sent in a POST request to the PHP script. upload_max_filesize is the maximum file size allowed via any method.

validating uploaded file size and type in php

I want to allow only PDF and MS word files and size must be less than 2MB,
here is my code:
$mimes = array(
'application/pdf',
'application/x-pdf',
'application/acrobat',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
);
if(!in_array($_FILES['file']['type'], $mimes)) {
$msg1='<div class="alert alert-error">Invalid file format, Please choose only PDF or MS word files</div>';
} elseif($_FILES['file']['size']>2097152){
$msg2='<div class="alert alert-error">The file is too large,(must be < 2MB)</div>';
}
My problem is:
When I choose pdf file with size >2MB $msg1 is displayed instead of $msg2
I want to show $msg1 when file is not PDF or MS Word file ,
and $msg2 when file is >2MB
any help plz????
If u have
<form ...>
<input id='upload' name='upload'>
</form>
You must test $_FILES['upload']['size'] > 2097152
This is because.. Your first condition become wrong when you uploads the pdf file. when you trying to upload a file with greater than 2M size,
$_FILES['file']['type']
returns a null value. The reason lies in your php.ini file. go and find the line 'upload_max_filesize'. it will be probably sett to 2M.so due to this the type returns an empty string.

Codeigniter : mp4 video won't upload, no error

I'm creating a form to upload video file, but I have a strange error.
My form is like this:
<div style="color : red;"><?php echo $error;?></div>
<?php echo form_open_multipart('data_center/ajout_ressource/formulaire_video'); ?>
//various input text fields...
<input type="file" name="video">
<input type="submit" value="Ajouter cette ressource" />
</form>±
My method to get upload is (it's inside something to check if the rest of the form is ok):
$dir = './assets/video/';
$config['upload_path'] = $dir;
$config['allowed_types'] = 'avi|flv|wmv|mpeg|mp3|mp4';
$config['max_size'] = '102400';
$this->load->library('upload',$config);
if ( ! $this->upload->do_upload('video')){
$error = array('error' => $this->upload->display_errors());
$this->load->view('data_center/ajout_video', $error);
} else {
redirect('data_center/data_center/','refresh');
}
Concerning mime types, I think it's ok:
'mp4' => 'video/mp4',
'wmv' => array('video/wmv', 'video/x-ms-wmv', 'flv-application/octet-stream', 'application/octet-stream'),
'flv' => array('video/flv', 'video/x-flv', 'flv-application/octet-stream', 'application/octet-stream'),
'avi' => 'video/x-msvideo',
When I try to upload a mp4 file it automatically redirect me (although I'm not sure it's a redirection) to the form, unfilled and no error message, even if I comment the lines in case of upload failure not to load the view again or if the rest of the form is uncorrect (it should not attempt to upload the video and load the form pre-filled).
Whereas for example, if I put the wrong type of file, I can see the error message ('The filetype you are attempting to upload is not allowed' for example), or if the form is wrong I can see the error of the form.
Finally, I must add that I modified php.ini to authorize such big files, and that I did pretty much the same thing with a form for pictures (jpg,jpeg,png...) which works perfectly but also redirect me to an unfilled form if I try to upload a .mp4 file.
Edit: I just downloaded an flv video to try, and it uploaded perfectly fine, but its size was less than 8mb (default config), and a 40mb flv file had the same problem as the mp4 file, although I changed my config in php.ini for 100mb
Efectively you have to change two parameters in PHP ini file
post_max_size = 100M
upload_max_filesize = 100M
But you may also want to change the apache abuse protection parameter (100M)
LimitRequestBody 1073741824
And by another hand, PHP have a time limit too of 30 sec per script, so your script will die at 30 seconds of running.
You may also want to increase the time to be sure your script does not die meanwhile you are uploading, copying etc,
set_time_limit(600); // 10 minutos execution

What is causing the file upload limit issue in php?

I'm reading this line in Linux. However, when I'm echoing this in the browser, nothing shows up. Is there something wrong with how I used the echo line?
// relevant code snippets
$mypic = $_FILES['upload']['name'];
$temp = $_FILES['upload']['tmp_name'];
$type = $_FILES['upload']['type'];
/*$finfo=finfo_open(FILEINFO_MIME_TYPE);
$type=finfo_file($finfo,$temp);
finfo_close($finfo);*/
echo "<pre>"; print_r($_FILES);
echo $mypic."<br />";
echo $type."<br />";
echo $_FILES['upload']['error']."<br />";
echo var_dump($type)."<br />";
If you suspect something is wrong with how I'm handling file inputs in another file, I've included that php file in this link.
<form ENCTYPE="multipart/form-data" method="post" action="insert.php">
Name: <input type="text" name="name" maxlength="15" /><br />
Email: <input type="text" name="email" maxlength="30" /><br />
Password: <input type="password" name="password" maxlength="15" /><br />
Confirm Password: <input type="password" name="cpassword" maxlength="15" /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="10000">
Choose your picture: <input type="file" name="upload"><p>
<input type="submit" name="submit" value="Register" /><br />
<p>
<center><h3><?php include("links.php"); ?></h3></center>
</form>
Here is the printout that I'm seeing:
Array (
[upload] => Array
(
[name] => protest.jpg
[type] =>
[tmp_name] =>
[error] => 2
[size] => 0
)
) protest.jpg
2 string(0) ""
------------------Update as of 9:40 p.m. May 5, 2012-------------------------
I tried an icon and found no problems other than permissions settings (I think I can solve this on my own for the time being). However, I'm still stuck on setting the file size. I followed Peter Stuart's instructions and got the following printout:
Apparently, the file size limits in these two settings are more than enough to handle the original images I had (which are under 200 kb). I don't know what more I can do in this case.
The file type is empty for the same reason that the filesize is 0 and the error is 2.
From Error Messages Explained:
UPLOAD_ERR_FORM_SIZE Value: 2; The uploaded file exceeds the
MAX_FILE_SIZE directive that was specified in the HTML form.
You have your max size set to 10000, which is in bytes, so that's roughly 10Kb. If it's a photo taken on any modern digital cam (over 4mgpx) it will probably need to be at least ten times that size. Just leave out the max size for now until you get a rough average of the image size people are submitting. PHP has a max upload size of its own to avoid tying up the line for too long.
To avoid issues like this in the future (not knowing if the file upload was successul), you probably want to wrap your code in something like:
$upload_error[0] = "AOK";
$upload_error[1] = "Server says: File too big!";
$upload_error[2] = "Browser says: File too big!";
$upload_error[3] = "File upload didn't finish.";
$upload_error[4] = "Ummm.... You forgot the file.";
$upload_error[6] = "Oops. Webmaster needs to fix something.";
$upload_error[7] = "Server says: I'm stuffed. Email webmaster.";
$upload_error[8] = "Server says: Not gonna do it. Webmaster needs to fix something.";
if($_FILES['upload']['error'] == 0) {
//do your thing and move it to the database.
} else {
$error_num = $_FILES['upload']['error'];
echo $upload_error[$error_num];
}
I would check your PHP upload size limit in your PHP ini file. It can cause adverse problems :)
If you create or go into your php.ini file and make sure the settings are as follows:
upload_max_filesize = 5M
post_max_size = 5M
The order of settings are also matter, and it should be like
upload_max_filesize = 5M
post_max_size = 5M
And I always got maximum size error when post_max_size place before upload_max_filesize.
This problem has 7 years but I stopped with it without finding a clear procedure to understand it. This is How I controlled it:
The sintom was that I could upload SMALL images (less than 2MB) BUT not bigger than 2MB. It's important to identify perfectly the ERROR. In this case "UPLOAD_ERR_FORM_SIZE:" The sintom in the DEBUG was that $_FILES["image"]["type"] = "", (ridiculous) and I knew that was a .JPG image for sure.
SOLUTION: Using XAMPP, STOP it. Configure php.ini, go to "upload_max_filesize=2M" which means that the file you try to upload has a limit of 2 Megabytes, So you Will change it to (for example) 3M. After that, I started again XAMPP, and proceeded to upload an image of 2.5 MB, and was successful.
Im sorry but my status can't show images of configuration in this comment.

Categories