The script is supposed to check if there are any uploaded files but it skips over the whole part of code.
if($posts->save()) {
if($this->request->hasFiles() == true) {
$postId = $posts->id;
foreach($this->request->getUploadedFiles() as $file) {
$images = new Images();
$images->postId = $postId;
$explodedName = explode('.', $file->getName());
$extension = end($explodedName);
$images->extension = $extension;
if($images->save()) {
$file->moveTo('images/' . $postId . '/' . $images->imageId);
return $this->forward('posts/add');
} else {
$this->flash->error('There was an error with images.');
return $this->forward('posts/add');
}
}
}
$this->flash->success('Post successfully created.');
}
If I don't upload any file at all, since it's optional, it displays "Post successfully created". If I upload a file with post then it will automatically redirect to the index while still successfully creating post but not inserting image data.