问题
when i update all 3 images then it will work fine. if i dont update all 3 then its work fine . but it will not work if i update 1 and not other. even it will not work if i update 2 and not 1 . may be its my if else cond. error. but i cant fig it out. so plz help me thanks :)
public function edit($id=2) {
$this->helpers = array('TinyMCE.TinyMCE');
$this->layout = 'adminpanel';
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$this->layout = 'adminpanel';
//save data
if ($this->request->is(array('post', 'put'))) {
$this->Home->id = $id;
//Save image
if(is_uploaded_file($this->request->data['Home']['image_1']['tmp_name']))
{
$fileNameFull = $this->request->data['Home']['image_1']['name'];
$uploadFolder = "upload";
//full path to upload folder
$uploadPath = WWW_ROOT . $uploadFolder;
$oldFile = $uploadPath.'/'.$fileNameFull;
move_uploaded_file(
$this->request->data['Home']['image_1']['tmp_name'],$oldFile
);
$newFile = WWW_ROOT.'courseImages/thumb/'.$fileNameFull;
$image = new ImageResizeComponent();
$quality = 100; // image resize for thumb
$height = 40;
$width = 60;
$this->ImageResize->resize($oldFile, $newFile, 60,60,$quality);
$this->request->data['Home']['image_1'] = $fileNameFull;
}
if(is_uploaded_file($this->request->data['Home']['image_3']['tmp_name']))
{
$fileNameFull = $this->request->data['Home']['image_3']['name'];
$uploadFolder = "upload";
//full path to upload folder
$uploadPath = WWW_ROOT . $uploadFolder;
$oldFile = $uploadPath.'/'.$fileNameFull;
move_uploaded_file(
$this->request->data['Home']['image_3']['tmp_name'],$oldFile
);
$newFile = WWW_ROOT.'courseImages/thumb/'.$fileNameFull;
$image = new ImageResizeComponent();
$quality = 100; // image resize for thumb
$height = 40;
$width = 60;
$this->ImageResize->resize($oldFile, $newFile, 60,60,$quality);
$this->request->data['Home']['image_3'] = $fileNameFull;
}
if(is_uploaded_file($this->request->data['Home']['image_2']['tmp_name']))
{
$fileNameFull = $this->request->data['Home']['image_2']['name'];
$uploadFolder = "upload";
//full path to upload folder
$uploadPath = WWW_ROOT . $uploadFolder;
$oldFile = $uploadPath.'/'.$fileNameFull;
move_uploaded_file(
$this->request->data['Home']['image_2']['tmp_name'],$oldFile
);
$newFile = WWW_ROOT.'courseImages/thumb/'.$fileNameFull;
$image = new ImageResizeComponent();
$quality = 100; // image resize for thumb
$height = 40;
$width = 60;
$this->ImageResize->resize($oldFile, $newFile, 60,60,$quality);
$this->request->data['Home']['image_2'] = $fileNameFull;
}
else{//Img not uploaded
//**TWO GOLDEN LINES OF YOUR LIFE**
$tourForImg = $this->Home->findByid($id);
$this->request->data['Home']['image_1'] = $tourForImg['Home']['image_1'];
$this->request->data['Home']['image_2'] = $tourForImg['Home']['image_2'];
$this->request->data['Home']['image_3'] = $tourForImg['Home']['image_3'];
//**TWO GOLDEN LINES OF YOUR LIFE**
}
if ($this->Home->save($this->request->data)) {
$this->Session->setFlash(__('Unable to hppp,ee your schedule.'));
//Save image
$this->Session->setFlash('Your home slider image has been saved343434.', 'default',array('class' => 'alert alert-success'));
//$this->Session->setFlash(__('Your home has been updated.'));
return $this->redirect(array('controller'=>'homes','action' => 'edit'));
$this->Session->setFlash(__('Unable to update your Tour.'));
}
}
$home = $this->Home->findByid($id);
if (!$home) {
throw new NotFoundException(__('Invalid post'));
}
if (!$this->request->data) {
$this->request->data = $home;
}
}
echo $this->Form->create('Home',array('autocomplete' => 'off','enctype'=>'multipart/form-data'));
echo $this->Form->input('image_1', array('type' => 'file')); // then display images
//above same for 3 image
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->end('Save Post'); ?>
回答1:
use AND operator
if(is_uploaded_file($this->request->data['Home']['image_1']['tmp_name']) &&
is_uploaded_file($this->request->data['Home']['image_3']['tmp_name']) &&
is_uploaded_file($this->request->data['Home']['image_2']['tmp_name'])
)
{
//HERE Code FOR ALL 3 IMAGES u You have written in the above code WHOLE CODE
//Save image
$fileNameFull = $this->request->data['Home']['image_1']['name'];
$uploadFolder = "upload";
//full path to upload folder
$uploadPath = WWW_ROOT . $uploadFolder;
$oldFile = $uploadPath.'/'.$fileNameFull;
move_uploaded_file(
$this->request->data['Home']['image_1']['tmp_name'],$oldFile
);
$newFile = WWW_ROOT.'courseImages/thumb/'.$fileNameFull;
$image = new ImageResizeComponent();
$quality = 100; // image resize for thumb
$height = 40;
$width = 60;
$this->ImageResize->resize($oldFile, $newFile, 60,60,$quality);
$this->request->data['Home']['image_1'] = $fileNameFull;
$fileNameFull = $this->request->data['Home']['image_3']['name'];
$uploadFolder = "upload";
//full path to upload folder
$uploadPath = WWW_ROOT . $uploadFolder;
$oldFile = $uploadPath.'/'.$fileNameFull;
move_uploaded_file(
$this->request->data['Home']['image_3']['tmp_name'],$oldFile
);
$newFile = WWW_ROOT.'courseImages/thumb/'.$fileNameFull;
$image = new ImageResizeComponent();
$quality = 100; // image resize for thumb
$height = 40;
$width = 60;
$this->ImageResize->resize($oldFile, $newFile, 60,60,$quality);
$this->request->data['Home']['image_3'] = $fileNameFull;
$fileNameFull = $this->request->data['Home']['image_2']['name'];
$uploadFolder = "upload";
//full path to upload folder
$uploadPath = WWW_ROOT . $uploadFolder;
$oldFile = $uploadPath.'/'.$fileNameFull;
move_uploaded_file(
$this->request->data['Home']['image_2']['tmp_name'],$oldFile
);
$newFile = WWW_ROOT.'courseImages/thumb/'.$fileNameFull;
$image = new ImageResizeComponent();
$quality = 100; // image resize for thumb
$height = 40;
$width = 60;
$this->ImageResize->resize($oldFile, $newFile, 60,60,$quality);
$this->request->data['Home']['image_2'] = $fileNameFull;
//Here 3 img
}
else if(is_uploaded_file($this->request->data['Home']['image_1']['tmp_name']) &&
is_uploaded_file($this->request->data['Home']['image_3']['tmp_name'])
)
{
//HERE Code FOR `image_1` AND `image_3`
}
else if(is_uploaded_file($this->request->data['Home']['image_1']['tmp_name']) &&
is_uploaded_file($this->request->data['Home']['image_2']['tmp_name'])
)
{
//HERE Code FOR `image_1` AND `image_2`
}
else if(is_uploaded_file($this->request->data['Home']['image_3']['tmp_name']) &&
is_uploaded_file($this->request->data['Home']['image_2']['tmp_name'])
)
{
//HERE Code FOR `image_3` AND `image_2`
}
else if(is_uploaded_file($this->request->data['Home']['image_1']['tmp_name']))
{
//HERE Code FOR `image_1`
}
else if(is_uploaded_file($this->request->data['Home']['image_2']['tmp_name']))
{
//HERE Code FOR `image_2`
}
else if(is_uploaded_file($this->request->data['Home']['image_3']['tmp_name']))
{
//HERE Code FOR `image_3`
}
else{//Img not uploaded
//HERE YOUR part You had WHEN image is NOT Uploaded
}
//Updated as per Your issue-> SEE , check 1st ELSE IF :
else if(is_uploaded_file($this->request->data['Home']['image_1']['tmp_name']) &&
is_uploaded_file($this->request->data['Home']['image_3']['tmp_name'])
)
{
//NOW u hav VALUES FOR `image_1` AND `image_3` OK ? BUT `image_2` val missing.SO set OLD VALUE from database
$tourForImg = $this->Home->findByid($id);
$this->request->data['Home']['image_2'] = $tourForImg['Home']['image_2'];
}
Now for next
else if(is_uploaded_file($this->request->data['Home']['image_1']['tmp_name']) &&
is_uploaded_file($this->request->data['Home']['image_2']['tmp_name'])
)
{
//NOW u hav VALUES FOR image_1
AND image_2
OK ? BUT image_3
val missing.SO set OLD VALUE from database
$tourForImg = $this->Home->findByid($id);
$this->request->data['Home']['image_3'] = $tourForImg['Home']['image_3'];
}
DO SAME FOR -> if image1 is only uploaded
Then set
OLD values of image2 & image3 from DATABASE
Do same for image2 only uploaded
Do same for image3 only uploaded .
GETTNG? use your own logic dude.
回答2:
First use before all if conditions
$tourForImg = $this->Home->findByid($id);
Use if else loop for each if
case in this way
// for image_1
if(yourconditions for image 1)
{
// Your code
$uploaded = true;
}
else
{
$this->request->data['Home']['image_1'] = $tourForImg['Home']['image_1'];
$uploaded = false;
}
you can also set a variable uploaded to check in end if no image was uploaded at all or you can also set count of images uploaded using $uploaded ++;
来源:https://stackoverflow.com/questions/26667829/multiple-image-editing-error-in-my-cakephp-project-cakephp2-5