Jenz
Jenz

Reputation: 8369

Cannot unlink file in Codeigniter

In my project I have a folder secure in root. The project package looks like:

application 
secure
system 
...........

Inside secure folder I am uploading some images on form submit using

$config1['upload_path'] = './secure/';
$ext = end(explode(".", $_FILES['thumb_image']['name']));
$config1['file_name'] = time().$_FILES['thumb_image']['name'];
$config1['allowed_types'] = 'jpg|png|jpeg|gif|bmp|jpe|tiff|tif';
$this->load->library('upload', $config1);
$this->upload->initialize($config1);
$this->upload->do_upload('thumb_image');

and it is working properly. Now while on editing the details, using another form, if I am uploading a new image instead of the current image file, I want to unlink the current one and then upload new file.

For this I am using the code:

unlink(base_url("secure/".$data['row']->videothumbnail));

I also tried with

unlink('/secure/'.$data['row']->videothumbnail);

where $data['row']->videothumbnail) is the current image file from database. New file is successfully uploaded. But old file is not getting unlinked. I have set the permission of secure folder to 777. But the images are uploaded with read only permission. Is it because of this, it is not getting unlinked?

Can anyone help me to solve this?

Thanks in advance.

Upvotes: 6

Views: 5031

Answers (11)

Barungi Stephen
Barungi Stephen

Reputation: 847

Though i came in late but someone might need this .

unlink(FCPATH."secure/".$data['row']->videothumbnail)

**FCPATH** - path to front controller, usually index.php
**APPPATH** - path to application folder
**BASEPATH** - path to system folder.

Upvotes: 0

Noushad Ranani
Noushad Ranani

Reputation: 15

if ($rowAffected > 0) {
                if ($isMediaUpload)
                    if (file_exists('./uploads/' . $this->input->post('img_url')))
                        unlink('./uploads/' . $this->input->post('img_url'));
                            redirect('/admin/configration', 'location');
            }

Upvotes: 0

Ankit
Ankit

Reputation: 11

First Load the $this->load->helper("file") and then unlink it

unlink("secure/".$data['row']->videothumbnail);

Upvotes: 0

Akhil Clement
Akhil Clement

Reputation: 685

use this to unlink

$oldthumb = "secure/".$data['row']->videothumbnail;
@unlink($oldthumb);

Upvotes: 0

user3024431
user3024431

Reputation: 461

I think you are just making a stupid mistake.

  • Firstly, the first param of unlink should be a relative path or absolute path, but base_url function will return you a path contains domain name, HOW CAN YOU DELETE A FILE ON REMOTE SERVER ?

  • Secondly, '/secure/'.$data['row']->videothumbnail here is not a relative path but a absolute path

YOU MUST change it into /the/absolute/path/to/secure/ or ./the/relative/path/to/secure/ (DO NOT MISS THE DOT)

Upvotes: 0

Velmurugan
Velmurugan

Reputation: 1

$unlinkUrl = "secure/".$data['row']->videothumbnail;
if(file_exists($unlinkUrl)){
    unlink($unlinkUrl);
}
else{
    echo $unlinkUrl." is not available";    
}

Upvotes: 0

Jessé Catrinck
Jessé Catrinck

Reputation: 2277

$this->load->helper("file") 
unlink(base_url('folder/file.ext'));

location:

\app\controller

\system\libraries

**folder\file.ext**

Upvotes: 0

Manikandan
Manikandan

Reputation: 41

Try to use $_SERVER['DOCUMENT_ROOT'] instead of base_url

Upvotes: 0

Jasnan
Jasnan

Reputation: 4514

I also had this issue even after setting the right permission on the folder. But the following code worked for me.

unlink(realpath(APPPATH . '../uploads').'/'.$ImageName);      

Upvotes: 0

Remya Murali
Remya Murali

Reputation: 226

Try this:

Set the permission dynamically using:

@chmod('./secure/'.$data['row']->videothumbnail, 0777);

then try unlink:

@unlink('./secure/'.$data['row']->videothumbnail);

Upvotes: 1

Hammad
Hammad

Reputation: 2137

Try echoing the path that you are providing to unlink function.

It should be something like this:

base_url()."secure/".$data['row']->videothumbnail;

Upvotes: 0

Related Questions