Deleting files using CodeIgniter?

11,402

As I posted in the comments, you could utilize PHP's unlink() function. However, this seems to return an E_WARNING error upon returning false. Like I also stated in the comments, you could utilize error suppression, but still handle the error in an appropriate manner, should it fail to delete a file.

Share:
11,402

Related videos on Youtube

Jakub
Author by

Jakub

Quality-focused IT Professional with many combined years of experience in roles as a Project Manager, Product Owner, Developer, Business analyst, and ScrumMaster. Established ability to deliver solutions that meet corporate objectives tied to business and technology performance. Creative analytical individual skilled at resolving business and technology challenges. My spare time is filled with tinkering, hardware and software alike, my passion lately lies with learning new things related to flying 'stuff' (Drones, RC Planes and RC Wings), technologies in the DIY space (RaspberryPi & Arduino based hardware) as well as 3D modeling and 3D printing. My past development background covers PHP, Python, Javascript (jQuery, AngularJS), HTML, CSS, .NET, and various SQL & NOSQL DB technologies. Want to know more? Contact me!

Updated on June 04, 2022

Comments

  • Jakub
    Jakub almost 2 years

    I'm not sure if this is the proper function to delete a single file (say an image) using CodeIgniter.

    $this->load->helper('file');    
    delete_files('path')
    

    http://codeigniter.com/user_guide/helpers/file_helper.html

    However it lists that this function is to delete entire directories, and makes no mention of how it handles individual files (if a path to only 1 file is given). Before I start testing with it, I figured I would check if anyone has ran into this problem before?

    I simply want to use a CodeIgniter function to delete individually uploaded images (say user profile images) but I am unable to find anything in the documentation / user guide that helps me achieve this (yes I know I need to have proper permissions first and all that, but that is out of scope of this question).

    regarding unlink()

    what I was hoping for is a built in CI function that would alert me to something stupid like "no delete permission" or other factors like "file is in use". I found unlink() to sometimes not work (without throwing me an error). Hence why I asked...

    • Russell Dias
      Russell Dias about 13 years
      What's wrong with using unlink() in PHP?
    • zerkms
      zerkms about 13 years
      @Russell Dias: unlink cannot delete recursively, at least.

Related