CodeIgniter Forums
Unlink Images and styles foreach loop - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Unlink Images and styles foreach loop (/showthread.php?tid=75076)



Unlink Images and styles foreach loop - rmcdahal - 12-17-2019

Here is my delete controller function 
Code:
public function DeleteById($id)
    {

        $file = $this->MediaModel->find($id);

        if (empty($file))
            return redirect()->back();
        $file_path = $this->config->storagePath . $file['upload_path'] . '/' . $file['media_name'];
        $small_path = $this->config->storagePath . $file['upload_path'] . '/small/' . $file['media_name'];
        $medium_path = $this->config->storagePath . $file['upload_path'] . '/medium/' . $file['media_name'];
        $extra_medium_path = $this->config->storagePath . $file['upload_path'] . '/extramedium/' . $file['media_name'];
        $all_files = array($file_path, $small_path, $medium_path, $extra_medium_path);
        // print_r($all_files);
        // die;
        // $result = $this->MediaModel->delete($id);        
        foreach ($all_files as $filpath) {
            if (is_readable($filpath)) {
                unlink($filpath);
                $this->MediaModel->delete($id);
            }
            return redirect('admin/media')->with('message', 'Image Deleted Successfully From Database');
        }
        return redirect('admin/media')->with('message', 'Image Delete Errors');
    }

From this code only main files get deleted but styles files remains undeleted, what i am missing ?