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.

Jenz
  • 8,280
  • 7
  • 44
  • 77

11 Answers11

1

Try this:

Set the permission dynamically using:

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

then try unlink:

@unlink('./secure/'.$data['row']->videothumbnail);
Remya Murali
  • 226
  • 1
  • 2
  • 11
0

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

It should be something like this:

base_url()."secure/".$data['row']->videothumbnail;
Hammad
  • 2,097
  • 3
  • 26
  • 45
0

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

unlink("secure/".$data['row']->videothumbnail);
Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
Ankit
  • 11
  • 3
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);      
Jasnan
  • 4,054
  • 2
  • 18
  • 23
0

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

Manikandan
  • 41
  • 8
0
$this->load->helper("file") 
unlink(base_url('folder/file.ext'));

location:

\app\controller

\system\libraries

**folder\file.ext**

Jessé Catrinck
  • 2,227
  • 19
  • 20
0
$unlinkUrl = "secure/".$data['row']->videothumbnail;
if(file_exists($unlinkUrl)){
    unlink($unlinkUrl);
}
else{
    echo $unlinkUrl." is not available";    
}
julienc
  • 19,087
  • 17
  • 82
  • 82
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)

user3024431
  • 461
  • 4
  • 8
0

use this to unlink

$oldthumb = "secure/".$data['row']->videothumbnail;
@unlink($oldthumb);
Akhil Clement
  • 575
  • 1
  • 7
  • 17
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');
            }
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.
Barungi Stephen
  • 759
  • 8
  • 13