2
2枚の画像を一度にアップロードしようとしていますが、特定の画像ファイルが特定のフォルダに保存されていますが、なんらかの理由で適切な画像が表示されません。アップロードと表示の両方で最初の画像のみが再生されます。これを修正する方法はありますか?CakePhP 3 - 同時に2つの画像ファイルをアップロードするには?
public function add()
{
$teamproject = $this->Teamproject->newEntity();
if ($this->request->is('post')) {
if (!empty($this->request->data['mainimg'])) {
$file = $this->request->data['mainimg'];
$destinationPath = WWW_ROOT . 'project_img' . DS;
$filename = $this->request->data['mainimg']['name'];
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$arr_ext = array('jpg', 'jpeg','png', 'JPG');
if (!in_array($extension, $arr_ext)) {
$this->Flash->error(__('Incorrect Image Format.. Please Try Again'));
}else{
$uniqueFileName = time().'_'.mt_rand(10000000, 99999999).'_'.mt_rand(10000000, 99999999).'.'.$extension;
move_uploaded_file($file['tmp_name'], $destinationPath . '/' . $uniqueFileName);
$filePath = '/project_img/' . $uniqueFileName;
}
}
if (!empty($this->request->data['imgone'])) {
$file = $this->request->data['imgone'];
$destinationPath = WWW_ROOT . 'imgone_img' . DS;
$filename = $this->request->data['imgone']['name'];
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$arr_ext = array('jpg', 'jpeg','png', 'JPG');
if (!in_array($extension, $arr_ext)) {
$this->Flash->error(__('Incorrect Image Format.. Please Try Again'));
}else{
$uniqueFileName = time().'_'.mt_rand(10000000, 99999999).'_'.mt_rand(10000000, 99999999).'.'.$extension;
move_uploaded_file($file['tmp_name'], $destinationPath . '/' . $uniqueFileName);
$filePath = '/imgone_img/' . $uniqueFileName;
}
}
$teamproject = $this->Teamproject->patchEntity($teamproject, $this->request->data);
$teamproject->mainimg = $filePath;
$teamproject->imgone = $filePath;
if ($this->Teamproject->save($teamproject)) {
$this->Flash->success(__('The teamproject has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The teamproject could not be saved. Please, try again.'));
}
$users = $this->Teamproject->Users->find('list', ['limit' => 200]);
$this->set(compact('teamproject', 'users'));
$this->set('_serialize', ['teamproject']);
}
私は、この問題は、あなたが画像を保存しながら、ループを実行して、イメージ名の末尾にループインデックスを連結する必要が
$teamproject = $this->Teamproject->patchEntity($teamproject, $this->request->data);
$teamproject->mainimg = $filePath;
$teamproject->imgone = $filePath;
foreachループのような意味ですか?どのくらい正確に?私はPHPでコーディングするのは本当に新しいです。それが理由です。 –
はい。 Foreachは作業を行います。値が1の "iterator"変数を宣言し、イメージを持つ配列上でforeachループを実行します。そのイテレータをイメージ名で連結し、最後に++をイテレータ変数と連結します。 forループも使用できます。 – codvlpr