CodeIgniter - file deletion, launch problem

I have 3 folders in my root, “application”, “system” and “upload”. In application / controllers / mycontroller.php, I have this line of code.

delete_files("../../uploads/$file_name"); 

The file is not deleted, and I tried several options for the path, for example ../ and ../../../ any ideas? Thanks.

+2
source share
6 answers

$file_name is a variable. You must combine it with your own line to execute the function:

 delete_files("../../uploads/" . $file_name); 

EDIT:

Make sure this sentence:

 echo base_url("uploads/" . $file_name); 

Refers to the right path. If the answer is YES, try the following:

 $this->load->helper("url"); delete_files(base_url("uploads/" . $file_name)); 

Suppose your "uploads" folder is in the root directory.

EDIT 2:

Using the unlink function:

 $this->load->helper("url"); unlink(base_url("uploads/" . $file_name)); 
+1
source

Use the FCPATH constant provided by CodeIgniter for this.

 unlink(FCPATH . '/uploads/' . $filename); 

base_url() generates HTTP URLs and cannot be used to create file system paths. This is why you should use one of the CI path constants. They are defined in the front controller file (index.php).

You will use three of them:

  • FCPATH - path to the front controller, usually index.php
  • APPPATH - path to the application folder
  • BASEPATH - path to the system folder.
+6
source

Try this ... it's just a very simple solution to your problem. If you notice that CI has a base_path definition for your directory, for example. in the download library configuration:

 $imagePath = './picture/Temporary Profile Picture/'; $config['upload_path'] = $imagePath; $config['allowed_types'] = 'gif|jpg|jpeg|png'; $this->load->library('upload', $config); 

if you notice that upload_path is set to .. / picture / Temporary Profile Picture / '

therefore, if you want to remove a file from the directory, all you have to do is use the unlink () function.

 unlink($imagePath . $file_name); 

or

 @unlink($imagePath . $file_name); 

Enjoy .. ^^

+1
source

This code worked for me. Try this on a model or controller. Change the file path according to yours.

file path →> project_name / assets / uploads / file_name.jpg

 public function delete_file() { $file = 'file_name.jpg'; $path = './assets/uploads/'.$file; unlink($path); } 
+1
source

You should try this code:

 $imagepath = $config['upload_path']; unlink($imagepath . $images); 

or

 delete_files($imagepath . $images); 
0
source
  public function deleteContent($id) { $this->db->where('Filename',$id); $this->db->delete('tableName',array('Filename'=>$id)); if (unlink("upload/folderName/".$id)) { redirect($_SERVER['HTTP_REFERER']); } } 
0
source

All Articles