0

そのため、データを CakePHP に保存する際に問題が発生しています。

画像をアップロードすると (['PictureForm']['file']['name'] が存在することを意味します)、すべて正常に動作し、データが保存されます。ただし、['PictureForm']['file']['name'] が null で、['PictureForm']['url'] が存在する場合、画像はディスクに正しく保存されますが、$this->PictureForm ->save($this->data) は失敗します。

私のコードに露骨に問題がある人はいますか?

if (isset($this->data['PictureForm'])) {
                            ///////////////////////////////////////////////////////DEV
                            if(!$this->data['PictureForm']['file']['name']) {
                                    if (!$this->data['PictureForm']['url']) {
                                            $this->Session->setFlash('Error: no image URL or file given!');
                                            $this->redirect(array('action' => '/'));
                                    } else {
                                            $fileExtension = getExtension($this->data['PictureForm']['url']);
                                            $this->request->data['PictureForm']['filename'] = randFilename() . "." . $fileExtension;
                                            file_put_contents('files/picture/' . $this->data['PictureForm']['filename'], file_get_contents($this->data['PictureForm']['url']));
                                    }
                            } else { //file was uploaded
                                    $fileExtension = getExtension($this->data['PictureForm']['file']['name']);
                                    if (!$fileExtension) {
                                            $this->Session->setFlash('Error: no file extension!');
                                            $this->redirect(array('action' => '/'));
                                    }
                                    $this->request->data['PictureForm']['filename'] = randFilename() . "." . $fileExtension;
                                    move_uploaded_file($this->data['PictureForm']['file']['tmp_name'], "files/picture/" . $this->data['PictureForm']['filename']);
                            }
                            $this->request->data = Sanitize::clean($this->request->data, array('encode' => false));
                            if ($this->PictureForm->save($this->data)) {
                                    resizeUpload($this->data['PictureForm']['filename']);
                                    $this->Session->setFlash('Picture <a href="/spootur/media/p/' . $this->data['PictureForm']['filename'] . '">saved</a>');
                                    $this->redirect(array('action' => 'p/' . $this->data['PictureForm']['filename']));
                            } else {
                                    $this->Session->setFlash('Error: could not save to db' . $this->data['PictureForm']['filename']);
                                    $this->redirect(array('action' => '/'));
                            }
                    }
4

1 に答える 1