We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Redirection without having any actual redirection code

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.



16.3k
edited Jun '14

Hmm, strange. maybe you can post more detail of the code.

just suggestion,

            if($images->save()) {
                $file->moveTo('images/' . $postId . '/' . $images->imageId);
                return $this->forward('posts/add');
            }

may be to check if file has moved successfuly then save to database is better. or may be

            if($images->save() && $file->moveTo('images/' . $postId . '/' . $images->imageId)) {
                return $this->forward('posts/add');
            }