Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete uploaded files with Codeigniter?

I used the Codeigniter's Upload Class to upload images to a folder in the project. In the database I only store the the url generated after upload the image, so when I want to delete a row in the db I also need to delete the image. How can I do it in codeigniter?

I will be grateful for your answers.

like image 936
user2065593 Avatar asked Feb 15 '13 17:02

user2065593


2 Answers

You can delete all the files in a given path, for example in the uploads folder, using this deleteFiles() function which could be in one of your models:

$path = $_SERVER['DOCUMENT_ROOT'].'/uploads/';

function deleteFiles($path){
    $files = glob($path.'*'); // get all file names
    foreach($files as $file){ // iterate files
      if(is_file($file))
        unlink($file); // delete file
        //echo $file.'file deleted';
    }   
}
like image 154
tim peterson Avatar answered Oct 22 '22 21:10

tim peterson


delete_row_from_db(); unlink('/path/to/file');

/path/to/file must be real path.

For eg :

if your folder is like this htp://example.com/uploads

$path = realpath(APPPATH . '../uploads');

APPPATH = path to the application folder.

Its working...

like image 39
Mansoorkhan Cherupuzha Avatar answered Oct 22 '22 20:10

Mansoorkhan Cherupuzha