0

I am using code-igniter. In my project i would like to delete the specific image in folder(artical_images). If i try to use delete_files() option it deletes all the images in the folder when use the code like,

public function delete_post($value='$id')
    {

        $query=$this->db->query("select artical_profile from articals where artical_id='$value' ");

        $img_name=$query->result()[0]->artical_profile;

        //echo $img_name;

        delete_files('./artical_images/',TRUE);

       // $this->db->where('artical_id',$value);
        //$this->db->delete('articals');

        exit();

    }

But when i include the specific image name taken from db and include with delete file path it doesn't work.The code is below,

public function delete_post($value='$id')
    {

        $query=$this->db->query("select artical_profile from articals where artical_id='$value' ");

        $img_name=$query->result()[0]->artical_profile;

        //echo $img_name;

        delete_files('./artical_images/$img_name',TRUE);

       // $this->db->where('artical_id',$value);
        //$this->db->delete('articals');

        exit();

    }

even i included the realpath option it also not works,

public function delete_post($value='$id')
    {

        $query=$this->db->query("select artical_profile from articals where artical_id='$value' ");

        $img_name=$query->result()[0]->artical_profile;

        $img_name=realpath(APPPATH . '/artical_images/$img_name');

        delete_files('$img_name',TRUE);

        echo $img_name;

       // $this->db->where('artical_id',$value);
        //$this->db->delete('articals');

        exit();

    }

please give some idea to delete the files,

2
  • Check if $img_name contains the proper image name or not and if you have the permission to delete the file or not? Commented Aug 16, 2016 at 6:00
  • ya.image name is same as what is in db. Commented Aug 16, 2016 at 6:03

2 Answers 2

1
$this->load->helper("file");
delete_files($path);
Sign up to request clarification or add additional context in comments.

5 Comments

yes, i auto load the helper "file".But it not works.
otherwise use unlink(FILE_PATH) this function
A PHP Error was encountered Severity: Warning Message: unlink(C:/xampp/htdocs/blog_creation/artical_images/artical_images/nboopathilb@gmail_com.jpg): No such file or directory i include the path and what the image name in folder but it shows as no such file or directory.why????
$filepath=FCPATH ."upload path/image name"; unlink($filepath); try like this one
thank you very much rajen antarkar i got it...from your answer.
1

delete_files uses a directory path to delete all the files in the directory. It applies the recursive unlink command to delete files.

To delete particular file, You can use unlink with the proper file path.

e.g. unlink(FILE_PATH);

8 Comments

is i want to take the application path or not??or i want to simply add the folder name and image name onl,in the place of file path.
Use full path of the file using APPPATH.
unlink('C:/xampp/htdocs/blog_creation/artical_images/artical_images/'.$img_name); i included the full path where the img stored . its not working
is $image_name contains file extension?
@BoopathiN You can accept answer if it works for you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.