Cannot unlink file in Codeigniter

2019-04-28 09:12发布

问题:

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.

回答1:

Try this:

Set the permission dynamically using:

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

then try unlink:

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


回答2:

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

It should be something like this:

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


回答3:

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);      


回答4:

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



回答5:

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

location:

\app\controller

\system\libraries

**folder\file.ext**



回答6:

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


回答7:

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)



回答8:

use this to unlink

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


回答9:

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

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


回答10:

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');
            }


回答11:

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.