Deleting a zip file using codeigniter - codeigniter

I want delete a zip file using codeigniter
This is my controller file:
public function delete($id)
{
$this->db->select('*');
$this->db->from('ag_angel1');
$this->db->where('angel1_id',$id);
$this->db->join('ag_file','ag_file.file_id = ag_angel1.angel1_id','left');
$ag=$this->db->get()->result();
foreach($ag as $g){
$path=realpath('uploaded/angel1/'.$g->file_name);
if (file_exists($path)) {
$this->load->helper('file');
delete_files($path) or die('failed deleting: ' . $path);
}else{
$this->load->helper('file');
unlike($path);
}
}
Code in model:
function delete($id)
{
if(!$id==null){
$this->db->where('angel1_id',$id);
if($this->db->delete('ag_angel1'))
{
return true;
}
}
}
But it doesn't work.
What is wrong?

$this->db->delete is used to delete data from a databse to delete a file use unlink
<?php unlink(server_path_to_your_file);?>
use this code in your model instead
function delete($id)
{
if(!$id==null){
$this->db->where('angel1_id',$id);
if(unlink('path_to_your_file'))
{
return true;
}
}
}

Related

Why squizlabs/php_codesniffer marked as error code with(new Vote)?

I laravel 9 project with squizlabs/php_codesniffer my phpstorm 2021 shows error :
Expected parameter of type '\TValue', 'Vote' provided
in model when I use table name in scope condition :
class QuizQualityResult extends Model
{
protected $table = 'quiz_quality_results';
public function scopeGetByVoteCategories($query, $voteCategoryId= null)
{
// “new Vote” is marked as error
$voteTable = with(new Vote)->getTable();
if (!empty($voteCategoryId)) {
if ( is_array($voteCategoryId) ) {
$query->whereIn( $voteTable . '.vote_category_id', $voteCategoryId);
} else {
$query->where( $voteTable . ' . vote_category_id', $voteCategoryId);
}
}
return $query;
}
If there is a way to fix this error ? Or maybe to use better syntax here ?
Thanks!
There is no need for helper with() here
$voteTable = (new Vote())->getTable()
Ps: there is a feeling that your method does not work the way you intended. Perhaps you meant to do the following (I could be wrong):
public function scopeGetByVoteCategories($query, $voteCategoryId = null)
{
if (empty($voteCategoryId)) {
return $query;
}
return $query->whereHas('vote', static function ($query) use ($voteCategoryId) {
if (is_array($voteCategoryId)) {
return $query->whereIn('vote_category_id', $voteCategoryId);
}
return $query->where('vote_category_id', $voteCategoryId);
});
}
public function vote()
{
// your relationship
}

Storage disk delete image from db

I have this error when i trying to delete an image from db:
Undefined variable: removeslider
public function delete($id){
$getslider = DB::table('slider')->where('id',$id)->get();
foreach($getslider as $getslider) {
$removeslider = $getslider->bgimage;
}
Storage::disk('uploadssliders')->delete($removeslider);
return redirect('admin/inicio');
}
Try this:
public function delete($id)
{
$getslider = DB::table('slider')->where('id',$id)->first();
if($getslider){
Storage::disk('uploadssliders')->delete($getslider->bgimage);
return redirect('admin/inicio');
} else {
//id has no match in the database
echo "the id ". $id . " does not exist";
}
}
I changed your get() to first() to only get one result as this seems what you are trying to do then the foreach is not needed.

how to update file in laravel

I update two data one text data and two file data but it's not working this. How to update file?
My code is:
public function update(Request $request){
$news=new News();
$news->newstitle=$request->newstitle;
$url1=$this->imageExistStatus1($request);
$news->save();
return redirect()->back()->with('sms','insert successful');
}
public function imageExistStatus1($request){
$newsByid1=News::where('id',$request->newsid)->first();
$fimage1=$request->file('imageone');
if ($fimage1) {
unlink($newsByid1->imageone)
$thisName1= $fimage1->getClientOriginalName();
$uplodePath1='public/up/';
$fimage1->move($uplodePath1,$thisName1);
$url1=$uplodePath1.$thisName1;
}else{
$url1=$newsByid1->imageone;
}
return $url1;
}
First, change your code to the following
public function update(Request $request, $id)
{
$news = News::findOrFail($id);
$news->newstitle = $request->newstitle;
$fimage1 = $request->file('imageone');
if ($fimage1) {
unlink($news->imageone);
$news->imageone = $this->imageExistStatus1($request, $id);
}
$news->save();
return redirect()->back()->with('sms', 'insert successful');
}
public function imageExistStatus1($request)
{
$fimage1 = $request->file('imageone');
$thisName1 = $fimage1->getClientOriginalName();
$uplodePath1 = 'public/up/';
$fimage1->move($uplodePath1, $thisName1);
$url1 = $uplodePath1 . $thisName1;
return $url1;
}
I hope your problem is resolved
tip:
Thanks to "Oluwatobi Samuel Omisakin".
please use the route:
Route::patch('/news/{id}/update', 'NewsController#update')

simple ajax form in cakephp 3.0

As JsHelper is no more in cakephp 3.0 so what i am doing is to save my form data into database using ajax
i just have two input fields.
my files are:
add.ctp
js.js
EmployeesController.php
add.ctp
$this->Form->create('Employees');
$this->Form->input('name', array('id'=>'name'));
$this->Form->input('age', array('id'=>'age'));
$this->Form->button('Add Info', array(
'type'=>'button',
'onclick'=>'infoAdd();'
));
$this->Form->end();
js.js
function infoAdd() {
var name=$("#name").val();
var age=$("#age").val();
$.get('/employees/info?name='+name+"&age="+age, function(d) {
alert(d);
});
}
EmployeesController.php
class EmployeesController extends AppController {
public $components=array('RequestHandler');
public function add() {
$emp=$this->Employees->newEntity();
if($this->request->is('ajax')) {
$this->autoRender=false;
$this->request->data['name']=$this->request->query['name'];
$this->request->data['age']=$this->request->query['age'];
$emp=$this->Employees->patchEntity($emp,$this->request->data);
if($result=$this->Employees->save($emp)) {
echo "Success: data saved";
//echo $result->id;
}
else {
echo "Error: some error";
//print_r($emp);
}
}
}
}
Note : my model only have not empty rule for both fields
all what i am doing is working fine but i dont think i m doing it in right way or as it should be.
please help me what i m missing and what i don't need to do.
Take away the autoRender line and serialize the data you want returned:
public function add() {
$data = [];
$emp=$this->Employees->newEntity();
if($this->request->is('ajax')) {
$this->request->data['name']=$this->request->query['name'];
$this->request->data['age']=$this->request->query['age'];
$emp=$this->Employees->patchEntity($emp,$this->request->data);
if($result=$this->Employees->save($emp)) {
$data['response'] = "Success: data saved";
//echo $result->id;
}
else {
$data['response'] = "Error: some error";
//print_r($emp);
}
}
$this->set(compact('data'));
$this->set('_serialize', 'data');
}
The serialize function tells Cake that it's not expecting the function to have a view, so autoRender is not needed (http://book.cakephp.org/3.0/en/views/json-and-xml-views.html).

Fetching “id” from a view and pass to another view in Grocery Crud CodeIgniter

I am new in Grocery Crud with Code Igniter and need a help. I have table vaboteni (emploees) and it work well. But I get stuck with code add more action. When I click to add action button I got error 404 Page Not Found. I want to fetch "id" from one row in table and pass to another view in order to display data for only one employee. I have site in local server, address localhost/bis_resursi/index.php/vraboteni/vraboteni_managment
Here is my Controller vraboteni.php
function vraboteni_management()
{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('vraboteni');
$crud->set_subject('вработен');
.....
$crud->add_action('Преглед', '', 'vraboteni/vraboten_managment/pregled','ui-icon-plus');
function pregled($id)
{
$this->load->model("vraboteni_pregled_model");
$data["result"] = $this->getVraboteniPregled($vrabotenID);
$this->load->view("pregled", $data);
}
$output = $crud->render();
$this->_example_output($output);
}
and Models: vraboteni_pregled_model.php
<?php
class Vraboteni_Pregled_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
function getVraboteniPregled($id){
$query = $this->db->query("SELECT * FROM vraboteni WHERE vraboteID = '$id' ");
return $query->result();
}
and in view vraboten_view.php I put
<?=$query['vrabotenID']?>
<br>
Hi, I'am <?=$query['ime']?>
<br>
from<?=$query['adresa']?>
I managed to find solution. Right code is:
Controller vraboteni.php
$crud->add_action('Преглед', '', 'vraboteni/get','ui-icon-plus');
$output = $crud->render();
$this->_example_output($output);
}
function vraboteni()
{
$crud = new grocery_crud();
$crud->set_table('vraboteni');
$output = $crud->render();
print_r($output);
}
function getall()
{
$this->load->model('vraboten_model');
$data['query']=$this->vraboten_model->vraboten_getall();
$this->load->view('vraboten_view',$data);
}
function get($vrabotenID)
{
$this->load->model('vraboten_model');
$data['query']=$this->vraboten_model->vraboten_get($vrabotenID);
$this->load->view('vraboten_view',$data);
}
Models vraboten_model.php
<?php
class Vraboten_model extends CI_Model{
function vraboten_model(){
parent::__Construct();
}
function vraboten_getall(){
$this->load->database();
$query=$this->db->get(' vraboteni');
return $query->result();
}
function vraboten_get($vrabotenID){
$this->load->database();
$query=$this->db->get_where(' vraboteni',array('vrabotenID'=>$vrabotenID));
return $query->row_array();
}
}
and view vraboten_view.php
<?=$query['vrabotenID']?>
<br>
Hi, I'am <?=$query['ime']?>
<br>
from<?=$query['adresa']?>

Resources