when i try to upload files with ajax and CI4 I have this error on the console
jQuery.ajax({
url: "PassagerController/addInformation",
method: "POST",
data : new FormData(this),
dataType:'json',
contentType: false,
cache: false,
processData: false,
the controller:
{
$passager = new PassagerModel();
$id = session()->get('telephone');
$numcni = $this->request->getPost('numcni');
$file = $this->request->getFile('rectocni');
if ($file->isValid() && ! $file->hasMoved()) {
$rectocni = $file->getRandomName();
$file->move(base_url().'/public',$rectocni);
}
then i allready put enctype=multipart/form-data
You are getting the error because you are trying to open a file over HTTP and expect it to be written on a local path. Remove "base_url()" and replace it with a local path.
As you are looking to store the file under the "public" directory, you can update the code to -
if ($file->isValid() && ! $file->hasMoved()) {
$rectocni = $file->getRandomName();
$file->move(ROOTPATH.'public', $rectocni);
}
"ROOTPATH" is a constant that has the path to the project root directory.
It is not good practice to store the uploaded file in the "public" directory, instead, look to place it under a sub directory under "writable".