11

I try to unlink an image in CodeIgniter, but the unlink function shows:

notice Undefined index: userfile

Here is my code

<?php
    function get_from_post(){
        $data['logo_name']  = $this->input->post('logo_name',TRUE);
        $data['logo_thumb'] = $_FILES['userfile']['name'];
        return $data;
    }

    function deleteconf($data){
        $data= $this->get_from_post();
        $update_id=$this->uri->segment(3);

        @unlink(base_url.'image/logo_thumb'.$logo_thumb);

        $query= $this->_delete($update_id);     
    }
?>
Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
Arul
  • 111
  • 1
  • 2
  • 9

11 Answers11

4

the unlink function shows notice Undefined index: userfile

The upload form, using multipart/form-data

Make sure you've use enctype="multipart/form-data" attribute/value for your upload form.

<form action="" method="post" accept-charset="utf-8" enctype="multipart/form-data">

From the MDN:

enctype
multipart/form-data: Use this value if you are using an <input> element with the type attribute set to "file".

If you're going to use CodeIgniter form helper, you could use form_open_multipart() function:

This function is absolutely identical to the form_open() tag above except that it adds a multipart attribute, which is necessary if you would like to use the form to upload files with.

Deleting files, File path vs URL address

PHP unlink() function accepts the Path of the file as the first argument. Not the URL Address.

The base_url() helper function returns the URL address of the site, which you've set in the config.php file.

You'll have to use the path of the file on your server, as follows:

unlink('/path/to/image/image_name.jpg'); // This is an absolute path to the file

You could use an Absolute or Relative path, but note that the relative path is relative to the index.php file. i.e. If the image/ folder is placed beside index.php file, you should use image/image_name.jpg as the file path:

unlink('image/image_name.jpg'); // This is a relative path to the file
Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
2

If you want to upload a photo for user profile and also at the time the old user photo should be deleted by using unlink method in codeignitor

$query = $this->db->where('profile_id',$profile_id)->get('users')->row();
$result = $query->photo;
$result = http://localhost/abc/user_photo/FE12563.jpg

if ($result) {
        $dd = substr($result, strlen(base_url()));
        unlink($dd);
        return  $this->db->set('photo',$photo)->where('profile_id',$profile_id)->update('users');
}
Vincent K
  • 1,326
  • 12
  • 19
0
function get_from_post(){
      $filename = $_POST['logo_name'];
      $path = $_SERVER['DOCUMENT_ROOT'].'/projectname/uploads/'.$filename ;
      if(is_file($path)){
        unlink($path);
        echo 'File '.$filename.' has been deleted';
      } else {
        echo 'Could not delete '.$filename.', file does not exist';
      }
  }
Reena Shirale
  • 1,992
  • 1
  • 17
  • 15
0

First check your file input name. Is it "userfile" or not? if not then add it and then run it once again.

Snehal S
  • 865
  • 4
  • 13
0

base_url() function returns url of you project but here you have to use directory path of file which you want to delete.

$path = BASEPATH.'/assets/upload/employees/contracts/'; 
$get_file = $path.$con_id.'.jpg'; 
 if(file_exists($get_file)){ 
 unlink($get_file); 
 }

instead of unlink(base_url("/assets/upload/employees/contracts/'.$con_id."));

Syed Ali
  • 151
  • 1
  • 4
0

First unlink function work only with path (without url), so please remove base_url() function.

Then in your first function $_FILES array doesn't contain userfile index, that's why getting error.

NOTE:- Before using unlink i would like to use also file_exists() function, first it will check if file is exist on same path then use unlink function (for error handling).

Like that -

<?php 
if(file_exists(filePath))
{
 unlink(filePath);
}

?>

Please fix both issue.

Thanks

Sorav Garg
  • 1,116
  • 1
  • 9
  • 26
0

Suppose you have the file name in the database and your file is abc.jpg. Then to unlink that in Code Igniter just do -

$file = "abc.jpg";
$prev_file_path = "./assets/uploads/files/".$file;
if(file_exists($prev_file_path ))
    unlink($prev_file_path );

My file upload path is "public_html/assets/uploads/files/". I am writing the above code in my controller which is "public_html/application/controllers/MyController.php". So the path will be same which I used in my CI file upload section. i.e.

...
$config['upload_path']   = './assets/uploads/files';
...

So we used relative path in both the upload and unlink section. So if the upload works perfectly then this will also work perfectly.

Sourav Purkait
  • 248
  • 2
  • 6
0

You can do like this, you need to get first the path of directory from where you have uploaded your image. Exemple:

//this is your url
$mainPath='https://google.com/uploads/image/16.jpeg';
$uploadedDirectory=str_replace("https://google.com/","./", $mainPath);
//now you get path like this ./uploads/image/16.jpeg
if(unlink($uploadedDirectory)) {
    $this->db->where('prodId',$id);
    $this->db->delete('products');
}
jona303
  • 1,358
  • 1
  • 9
  • 27
Asif
  • 1
  • 1
-1
$this->load->helper("file");

just add above line before unlink

unlink($path);
Kiran Patel
  • 369
  • 1
  • 5
-1

Can you please use this code to remove image on folder.

unlink(realpath('uploads/' . $_image));

"uploads" : Image exist in uploads folder. "$_image" : Image file name

PHP functions: 1 : unlink() 2 : realpath()

The above code successfully working on my site to delete image on folder.

-2

try this code

unlink(base_url().'/image/logo_thumb'.$logo_thumb);

Note: You didn't assign / declare $logo_thumb in your deleteconf().

Please check your code.

Kumar V
  • 8,810
  • 9
  • 39
  • 58
  • i try ed its shows notice Undefined index: userfile – Arul Feb 08 '14 at 06:00
  • I think you did not post full code. userfile variable doesn't exist in your code. Post full code clearly. Otherwise your question will be flagged as unclear. – Kumar V Feb 08 '14 at 06:02