Delete a file in codeigniter

Asked
Active3 hr before
Viewed126 times

7 Answers

delete
90%

1 if it gives you an error, you should silence it like this @unlink($path_to_file) But to make sure the file exists, use is_file function before deleting it. – machineaddict Mar 14 '13 at 14:34 , 3 Yes you're right. I looked it now and delete_files function uses a recursive unlink() to delete files in a specified folder. This question's answer should be updated. – Taha Paksu Dec 30 '12 at 11:55 ,CodeIgniter v4: http://codeigniter.com/user_guide/helpers/filesystem_helper.html#delete_files,CodeIgniter v3: http://codeigniter.com/userguide3/helpers/file_helper.html#delete_files

and like this :

$this - > load - > helper("file");
delete_files($path);

Late Edit: delete_filesmethod uses a path to wipe out all of its contents via unlink() and same you can do within CI. Like this:

unlink($path);
load more v
88%

CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications. ,   View a Printable Version

$path = base_url('assets/uploads/files/').$user - > file_url;
if (@unlink($path)) {
   echo "Deleted file ";
} else {
   echo "File can't be deleted";
}
72%

you can use the "file helper" in codeigniter.,CodeIgniter v3: http://codeigniter.com/userguide3/helpers/file_helper.html#delete_files,Late Edit: delete_filesmethod uses a path to wipe out all of its contents via unlink() and same you can do within CI. Like this:,CodeIgniter v4: http://codeigniter.com/user_guide/helpers/filesystem_helper.html#delete_files

and like this :

$this - > load - > helper("file");
delete_files($path);
load more v
65%

base_url() function returns url of you project but here you have to use directory path of file which you want to delete., bhargav 3 Years ago base_url() function returns url of you project but here you have to use directory path of file which you want to delete. $path = BASEPATH.'/assets/upload/employees/contracts/'; $get_file = $path.$con_id.'.jpg'; if(file_exists($get_file)){ unlink($get_file); } instead of unlink(base_url("/assets/upload/employees/contracts/'.$con_id.")); ,Success Delete from Database but Folder file and Image upload can't delete. ,instead of unlink(base_url("/assets/upload/employees/contracts/'.$con_id."));

This my code controllers "contract.php"

function delete($con_id) {
   //$year=$this->session->userdata('year');
   $path = ('/assets/upload/employees/contracts/');
   $get_file = $path.$con_id.
   '.jpg';
   $this - > db - > where('con_id', $con_id);
   $this - > db - > delete('sch_emp_contract');
   if (file_exists($get_file)) {
      unlink(base_url("/assets/upload/employees/contracts/'.$con_id."));
   }

   $m = '';
   $p = '';
   if (isset($_GET['m'])) {
      $m = $_GET['m'];
   }
   if (isset($_GET['p'])) {
      $p = $_GET['p'];
   }

   redirect("employee/contract?m=$m&p=$p");
}

This code View contract_list.php - Button Delete

<td width="1%" class="remove_tag">';
     if($this->green->gAction("D")){
     $tr .='<a title="Delete Contract" id="clk_del" class="clk_del">
<img rel="'.$contract['con_id'].'" src="'.site_url('../assets/images/icons/delete.png').'" onclick="delete_contrac (event);" style="width:20px;height:20px;"></a>';}$tr .='</td>

Function

function delete_contract(event) {
   var r = confirm("Are you sure to delete this record !");
   if (r == true) {
      var contr_id = $(event.target).attr('rel');
      location.href = "<?PHP echo site_url('employee/contract/delete');?>/" + contr_id + "?<?php echo "
      m = $m & p = $p " ?>";
   }
}
load more v
75%

public
function delete_attachment() {
   $id = $this - > input - > post('id');
   $this - > db - > select('*');
   $this - > db - > where('id', $id);
   $q = $this - > db - > get('attachments');
   $first = $q - > row();
   $this - > db - > delete('attachments', array('id' => $id));
   if ($this - > db - > affected_rows() >= 1) {
      $upload_path = './assets/user/images/'.$first - > path;
      if (unlink($upload_path)) {
         $this - > attachments_by_clients_web_id();
      }
   } else {
      $this - > attachments_by_clients_web_id();
   }
   return;
}
public
function attachments_by_clients_web_id() {
   $id = $this - > session - > userdata('id');
   $attachments = $this - > user_model - > attachments_by_clients_web_id($id);
   echo json_encode($attachments);
}
load more v
40%

For delete record we use 3 file here,Now i am going to delete the id=3 record.,Crud.php Path: codeIgniter\application\controllers\Crud.php,Crud_model.php Path: codeIgniter\application\models\Crud_model.php

CREATE TABLE crud(
   `id`
   int(11) AUTO_INCREMENT PRIMARY KEY NOT NULL,
   `first_name`
   varchar(30) NOT NULL,
   `last_name`
   varchar(30) NOT NULL,
   `email`
   varchar(30) NOT NULL
);
load more v
22%

Delete Directory in Codeigniter You can use file helper in codeigniter to delete files. Using this helper you can delete files in folder., Here in an example of deleting all files in folder-,It will delete all folders/files inside the images folder.,1. Distinct in Codeigniter Query

$path = $this - > config - > base_url().
'images';
$this - > load - > helper("file"); // load the helper
delete_files($path, true); // delete all files/folders inside images folder

Other "delete-undefined" queries related to "Delete a file in codeigniter"