cakephp 3.xアプリケーションでイメージをアップロードしようとしています。画像をアップロードしていないときは、他のデータはうまく保存されていましたが、ファイルアップロードの統合以来、画像のアップロード機能は正常に機能していますが、他のデータは保存されません。以下はサンプルコードです。見つけられない小さな問題しかないかもしれないと思うので、ご覧ください。ここでは、コントローラでの追加アクションのコードは次のとおりです。ここでCakePHP3でイメージのアップロードとデータの競合が発生する
$team_member = $this->TeamMembers->newEntity();
if ($this->request->is('post')) {
if (!empty($this->request->data['photo']['name'])) {
$photo = $this->request->data['photo'];
$ext = substr(strtolower(strrchr($photo['name'], '.')), 1); //get the extension
$arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions
$setNewFileName = time() . "_" . rand(000000, 999999);
//only process if the extension is valid
if (in_array($ext, $arr_ext)) {
//do the actual uploading of the file. First arg is the tmp name, second arg is
//where we are putting it
move_uploaded_file($photo['tmp_name'], WWW_ROOT . '/uploads/' . $setNewFileName . '.' . $ext);
//prepare the filename for database entry
$imageFileName = $setNewFileName . '.' . $ext;
}
}
$team_member = $this->TeamMembers->patchEntity($team_member, $this->request->data);
if (!empty($this->request->data['photo']['name'])) {
$team_member->photo = $imageFileName;
}
if ($this->TeamMembers->save($team_member)) {
$this->Flash->success(__('Saved Successfully.'));
return $this->redirect([
'action' => 'index'
]);
}
$this->Flash->error(__('Failed to save. Try Again.'));
}
$this->set('team_member', $team_member);
はadd.ctpファイルです:
echo $this->Form->create($team_member, ['enctype' => 'multipart/form-data']);
echo $this->Form->input('name');
echo $this->Form->input('photo', array('type' => 'file'));
echo $this->Form->input('excerpt');
echo $this->Form->input('content');
echo $this->Form->button('Save', ['class' => 'btn']);
echo $this->Form->end();
私は、ファイルの任意のコードを書いていなかったとき、以前のそれは正常に動作していることに注意してくださいアップロードする。そして、CakePHPのバージョンが3.xの
TIA
通常、 '$ this-> Form-> create($ team_member、['type' => 'file']))'が必要です。私は、生成されたフォームに適切なエンコーディングタイプが設定されていないとうまくいくのではないかと疑います。 ** http://book.cakephp.org/3.0/en/views/helpers/form.html#changing-the-http-method-for-a-form** – ndm
私はenctypeはここで定義する必要がありますが、まだenctypeを追加したり、['type' => 'file']を定義しても機能しません。私はこのヘルパーリンクを読んで何度も本を紹介してきましたが、まだそれはうまく機能していません。しかし、とにかく、この間違ったコードで、今働いています。それがなぜ起こるのか分かっているなら、私に知らせてください。 –