public function axUpload()
{
$posted = $this->request->getPost();
if(!array_key_exists("label", $posted) || !array_key_exists("file_input", $posted))
{
$data = [ 'success' => 'false', 'text' => 'No label or file input fieldname provided!' ];
$res = array_merge($data , ['statusText' => 'OK', 'request' => $posted]);
return $this->respond($res);
}
$label = $posted['label'];
$filefield = $posted['file_input'];
$type = $posted['type'];
$id = $posted['id'];
$file = $this->request->getFile($filefield); // CI File Object
$rules = [
$filefield => [
'label' => $label,
'rules' => [ 'uploaded['.$filefield.']', 'mime_in['.$filefield.',application/pdf]', 'max_size['.$filefield.',1500000]', ],
'errors' => [ 'uploaded' => 'File was not uploaded', 'mime_in' => 'Only PDF files are allowed', 'max_size' => 'The uploaded file is too big', ]
],
];
$validation = service('validation');
$validation->setRules($rules);
if(! $this->validateData([$file], $rules))
{
$errors = $validation->getErrors();
$data = [ 'success' => 'false', 'errors' => $errors, 'mime' => $file->getMimeType(), 'size' => $file->getSize() ];
$res = array_merge($data , ['statusText' => 'OK', 'request' => $posted]);
return $this->respond($res);
}
else
{
$fileExt = $file->getClientExtension();
$newName = $this->user->initials . '_' . date('Ymd_His') . '__' . strtolower($filefield) . '.' . strtolower($fileExt);
$path = 'uploads/fcwo/'.$this->user->initials.'/'.date('Ymd').'/';
// Move file to location and keep track of upload in DB
if ($file->isValid() && !$file->hasMoved())
{
$file->move(WRITEPATH.$path, $newName);
$data = [ 'success' => 'true', 'filename' => $newName, 'path' => $path.$newName, 'size' => $file->getSize() ] ;
// Save file info in DB, which table / type?
$model = $this->_createModelInstance($type); // creates corresponding model instance, if type = 'con', it returns the con_requests model
$entity = $model->find($id);
unset($entity->$filefield);
$entity->$filefield = json_encode(['filename' => $newName, 'path' => $path.$newName]); // all ok until here!
if ($entity->hasChanged()) // returns true
{
if($model->save($entity))
{
/* And here, the script stops ...
"Nesting level too deep - recursive dependency?
Previous Exception
CodeIgniter\\Database\\Exceptions\\DataException
There is no data to update.
#0 /earthweb2_ci4/CodeIgniter/system/BaseModel.php(1011): CodeIgniter\\Database\\Exceptions\\DataException::forEmptyDataset('update')
#1 /earthweb2_ci4/CodeIgniter/system/Model.php(865): CodeIgniter\\BaseModel->update(Array, Array)
#2 /earthweb2_ci4/CodeIgniter/system/BaseModel.php(749): CodeIgniter\\Model->update('6', Object(Modules\\Fcwo\\Entities\\ConEntity))
#3 /earthweb2_ci4/CodeIgniter/modules/Fcwo/Controllers/Fcwo.php(249): CodeIgniter\\BaseModel->save(Object(Modules\\Fcwo\\Entities\\ConEntity))
#4 /earthweb2_ci4/CodeIgniter/system/CodeIgniter.php(933): Modules\\Fcwo\\Controllers\\Fcwo->axUpload()
#5 /earthweb2_ci4/CodeIgniter/system/CodeIgniter.php(509): CodeIgniter\\CodeIgniter->runController(Object(Modules\\Fcwo\\Controllers\\Fcwo))
#6 /earthweb2_ci4/CodeIgniter/system/CodeIgniter.php(355): CodeIgniter\\CodeIgniter->handleRequest(NULL, Object(Config\\Cache), false)
#7 /earthweb2_ci4/CodeIgniter/system/Boot.php(325): CodeIgniter\\CodeIgniter->run()
#8 /earthweb2_ci4/CodeIgniter/system/Boot.php(67): CodeIgniter\\Boot::runCodeIgniter(Object(CodeIgniter\\CodeIgniter))
#9 /earthweb2_ci4/WWW/index.php(56): CodeIgniter\\Boot::bootWeb(Object(Config\\Paths))\n#10 {main}",
"file": "/earthweb2_ci4/CodeIgniter/system/Debug/Exceptions.php",
"line": 602,
*/
return $this->respond($data);
}
else
{
// Info not saved in DB, remove uploaded file!
if(file_exists(WRITEPATH.$path.$newName))
{
unlink(WRITEPATH.$path.$newName);
}
$data = [ 'success' => 'false', 'errors' => [$filefield => 'Record could not be updated, try again', 'path' => $path.$newName] ] ;
}
}
die("Nothing has changed!");
}
$data = [ 'success' => 'false', 'errors' => [$filefield => 'File upload failed, invalid file or failed to move to uploads', 'path' => $path.$newName] ] ;
}
$res = array_merge($data , ['statusText' => 'OK', 'request' => $posted]);
return $this->respond($res);
}