How to unlink (delete) an image in CodeIgn

2019-06-16 04:38发布

问题:

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

回答1:

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


回答2:

try this code

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

Note: You didn't assigned / declared $logo_thumb in your deleteconf().

Please check your code.



回答3:

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



回答4:

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


回答5:

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


回答6:

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



回答7:

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



回答8:

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.



回答9:

$this->load->helper("file");

just add above line before unlink

unlink($path);


回答10:

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.