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
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');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With