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);
}
?>
the unlink function shows notice Undefined index:
userfile
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.
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