-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: Codeigniter doesn't recognize file upload data for large files #8403
Comments
I cannot reproduce the problem. Can you provide the full logic of your example controller method? Also the The only scenario when it will not work is when the |
Hi @michalsn, thanks for the quick reply. You're right, I removed the redundant parameter from I've just tried again to upload a 9MB image with the following settings in upload_max_filesize = 20M
; Value may be 0 to disable the limit (ok, let's try that)
post_max_size = 0 But unfortunately it still doesn't work. Also, I just noticed that I also can't access POST data when trying to upload large files. So not only the FILES data is affected. Really weird! I'm using CodeIgniter version
Sure thing, here's a simplified version of my logic to create a new entry, I just removed some error checks and other unimportant stuff: public function create()
{
$product = new Product;
$formdata = $this->getFormData();
$product->fill($formdata["input"]);
$id = $this->model->insert($product);
if ($formdata["files_exist"]) {
$filestack = $this->upload($id, $formdata["files"]);
if ($filestack === false) {
return redirect()
->back()
->with("errors", ["File too large"]);
}
$product->image = implode(", ", $filestack);
$this->model->update($id, $product);
}
return redirect()
->to("products/$id")
->with("message", "Product saved.");
}
private function getFormData(?Product $product = null): array
{
$files = $this->request->getFiles();
$post_data = $this->request->getPost();
// for testing purposes only
dd($files);
$files_exist = isset($files["images"])
? $files["images"][0]->getSize() > 0
: false;
// insert "placeholder" to avoid "nothing to update" error
$filestack = ($product && $product->image)
? $product->image . ", placeholder"
: "placeholder";
$file_data = ["images" => $filestack];
$input = $files_exist
? array_merge($post_data, $file_data)
: $post_data;
return [
"files" => $files,
"files_exist" => $files_exist,
"input" => $input,
];
}
private function upload(string $id, array $files): array | bool
{
$filestack = [];
$file_too_large = false;
foreach ($files["images"] as $file) {
if (! $file->isValid()) {
$error_code = $file->getError();
if ($error_code !== UPLOAD_ERR_NO_FILE) {
throw new RuntimeException(
$file->getErrorString()." ".$error_code
);
}
}
// for testing purposes only
if ($file->getSizeByUnit("mb") > 2) {
$file_too_large = true;
break;
}
$filename = $file->getRandomName();
$file->move($this->upload_dir . "/products/$id/", $filename);
array_push($filestack, $filename);
}
return $file_too_large ? false : $filestack;
} Let me know if you need more information. |
I think the problem is your implementation. When the file size exceeds the allowed size set in php.ini, you will not get the expected value from methods like: |
It has nothing to do with that, I just added
I shared my updated You should also notice, that Also, may I ask why you removed the "bug" label? |
I focused on the original error that was reported, which was the absence of an error when
Please notice what values are returned when you check the results of
The "bug" label was removed because at this point I don't think this is a bug in the framework. As I mentioned earlier, I checked the code from your original report and it's working just fine.
There is nothing that would prevent that. Please make sure and call Overall, I would suggest to post this on the forum. You may find more help there since we won't be debugging your code here unless you provide a clear example for a bug. |
I've commented out all the code to show you what I mean: public function create()
{
dd($this->request->getPost());
} Now, when I fill out my form, I get the POST data as expected. However, when I do the same, but with the 9MB image I mentioned earlier, I get:
It's the same with In upload_max_filesize = 20M
post_max_size = 0 I am not asking for help to debug my code, I just find it strange that I cannot access any I also tested it with plain PHP and it works as expected; I always get the complete form data regardless of file size. |
Are you doing this in the same environment? What do you get when you call: dd(ini_get('upload_max_filesize'), ini_get('post_max_size'), ini_get('memory_limit')); |
Yes, it's the same environment.
I will now restore the settings as they were originally, as it doesn't make any difference in this particular case. I will see if I can get it to work on my secondary machine (same OS and server) when I have more time. |
The only thing I have found is this: https://www.php.net/manual/en/ini.core.php#ini.post-max-size
If you gonna test it more, please make some really simple methods that will only display data that has been sent, like: public function create()
{
dd($this->request->getFiles(), $this->request->getPost());
} And without using our methods (to see if there are any differences): public function create()
{
dd($_FILES, $_POST);
} If there will be no differences, please try also plain PHP (without framework): var_dump($_FILES, $_POST); Please, make your code the bare minimum when you write the test code. Also, please post settings for |
PHP Version
8.1
CodeIgniter4 Version
4.4.3
CodeIgniter4 Installation Method
Composer (using
codeigniter4/appstarter
)Which operating systems have you tested for this bug?
Linux
Which server did you use?
apache
Database
MySQL version 1.24.3
What happened?
According to the CodeIgniter 4.4.4 documentation on Working with uploaded files, the
isValid()
method should return an error, when trying to upload a file that exceeds theupload_max_filesize
defined inphp.ini
, which in my case is set to 2MB.When I try to upload a file larger than 2MB, it doesn't catch the error, more importantly, it doesn't recognize the
name
value of the given file input, so it seems that I can't check the file size manually either.For example, given the following code:
If the file is too large, I get the following error:
I've also tried using the
validate()
method as described in the documentation, but I get the same error.Looking for a solution, I came across this forum post where it is suggested to change the
upload_max_filesize
inphp.ini
. But I don't want to do that because not every hosting provider gives you the control to change that. However, for testing purposes, I changed it, restarted my Apache server and tried again, but I still get the same error.When I try the same thing in plain PHP, it works as expected because I can access the file data and thus check for the file size.
Steps to Reproduce
See "What happened?" section.
Expected Output
I should get an
UPLOAD_ERR_INI_SIZE
error message.Anything else?
Note that I've tested it on my local machine with Apache 2.0.
The text was updated successfully, but these errors were encountered: