2017-02-23 78 views
0

ниже код уже работает с ручным удалением, я хочу получать точное изображение, так же, как нижеКак удалить твердую копию изображения в каталоге с помощью CakePHP3

файла $ = новый файл (WWW_ROOT '. img/websites/image-todelete.jpg ', false, 0777);

public function delete($id = null) 
    { 

    $this->request->allowMethod(['post', 'delete']); 
    $website = $this->Websites->get($id);  

    if ($this->Websites->delete($website)) { 
     $file = new File(WWW_ROOT . 'img/websites/image-todelete.jpg', false, 0777); 
      if($file->delete()) { 
       $this->Flash->success(__('The website has been deleted.')); 
      } 

    } else { 
     $this->Flash->error(__('The website could not be deleted. Please, try again.')); 
    } 
    return $this->redirect(['action' => 'index']); 
} 

Как сделать этот код

$file = new File(WWW_ROOT . 'img/websites/image-todelete.jpg', false, 0777); 

универсальный код? Если я когда-либо удаляю данные, он должен автоматически удалять изображение в каталоге при удалении записи.

+0

Я думаю, что unlink() может это сделать. – Deep

+0

@unlink() - это функция – AAT

+0

Я хочу использовать код выше, используя id, чтобы img/websites/image-todelete.jpg быть переменной – distromob

ответ

0

Вы можете просто сделать это:

public function delete($id = null) 
{ 

$this->request->allowMethod(['post', 'delete']); 
$website = $this->Websites->get($id);  

// take destination just before deleting that 
$destination= $website->destination; 
if ($this->Websites->delete($website)) { 
    $file = new File($destination); 
     if($file->delete()) { 
      $this->Flash->success(__('The website has been deleted.')); 
     } 

} else { 
    $this->Flash->error(__('The website could not be deleted. Please, try again.')); 
    } 
    return $this->redirect(['action' => 'index']); 
} 

И ваш метод экономии:

public function add(){ 
$website = $this->Websites->newEntity(); 

if ($this->request->is('post')) { 


     //$image_name  = uniqid().$this->request->data['image']['name']; 
     $image_name  = $this->request->data['image']['name']; 
     $image_tmp   = $this->request->data['image']['tmp_name']; 
     $destination  = WWW_ROOT.'img'.DS.'websites'.DS.$image_name; 
     move_uploaded_file($image_tmp, $destination); 
     $this->request->data['image'] = $image_name; 
     $this->request->data['destination'] = $destination; 

     $website = $this->Websites->patchEntity($website, $this->request->data); 

     if ($this->Websites->save($website)) { 
      $this->Flash->success(__('Data has been saved.')); 
      return $this->redirect(['action' => 'index']); 
     } else { 
      $this->Flash->error(__(' Please, try again.')); 
     } 
    } 



    $this->set(compact('website')); 
    $this->set('_serialize', ['website']); 
} 

Не забудьте добавить поле поле 'назначения' в таблице ..

+0

он вернет это, когда я отлаживаю public_html/webroot/img/websites/Идентификатор ресурса # 179 ' , но запись уже удалена, а изображение все еще находится в каталоге @Manohar Khadka – distromob

+0

результат сообщения debug ($ filename) .. –

+0

/src/Controller/WebsitesController.php (строка 127) ресурс = это отладка ($ filename); – distromob

0

Попробуйте функцию unlink().

unlink(WWW_ROOT . 'img/websites/image-todelete.jpg');