Cannot unlink file in Codeigniter
Asked Answered
S

11

6

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.

Slipover answered 2/4, 2014 at 6:49 Comment(5)
thanks i missed . before /secure.Slipover
You could try do a chmod on the file after the upload to change the permissionsLongshore
if dir path wrong then unlink function show warning, can you show warning??Limbert
it is now working..the problem was I missed . before the /secure/ path..Slipover
You can have a look at this url #12321089Weinberger
P
1

Try this:

Set the permission dynamically using:

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

then try unlink:

@unlink('./secure/'.$data['row']->videothumbnail);
Polyclitus answered 2/4, 2014 at 7:5 Comment(0)
E
0

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

It should be something like this:

base_url()."secure/".$data['row']->videothumbnail;
Evergreen answered 2/4, 2014 at 7:1 Comment(2)
that still won't help because u cannot unlink from a URL!Longshore
Um - no it isn't... base_url will provide you HTTP path, the unlink function required directory path...Limbert
E
0

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

unlink("secure/".$data['row']->videothumbnail);
Empty answered 27/5, 2014 at 8:57 Comment(0)
K
0

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);      
Keyes answered 1/6, 2014 at 18:1 Comment(0)
P
0

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

Percutaneous answered 10/7, 2014 at 7:8 Comment(0)
E
0
$this->load->helper("file") 
unlink(base_url('folder/file.ext'));

location:

\app\controller

\system\libraries

**folder\file.ext**

Edie answered 11/7, 2014 at 18:49 Comment(0)
S
0
$unlinkUrl = "secure/".$data['row']->videothumbnail;
if(file_exists($unlinkUrl)){
    unlink($unlinkUrl);
}
else{
    echo $unlinkUrl." is not available";    
}
Strader answered 11/8, 2014 at 5:36 Comment(0)
P
0

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)

Perilune answered 30/8, 2014 at 6:51 Comment(0)
I
0

use this to unlink

$oldthumb = "secure/".$data['row']->videothumbnail;
@unlink($oldthumb);
Incubate answered 17/9, 2014 at 5:35 Comment(0)
F
0
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');
            }
Fumigant answered 23/7, 2015 at 5:34 Comment(0)
G
0

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.
Graybill answered 18/3, 2019 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.