Deleting a File using php/codeigniter

前端 未结 9 1883
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-04 07:47

I would like to delete a file that is found in my localhost.

localhost/project/folder/file_to_delete

I\'m using codeigniter for this.

相关标签:
9条回答
  • 2021-01-04 08:25

    Try this, this works for me:

    unlink("./path/to/folder/file_name_do_delete");
    

    for example: I put my file inside uploads folder which is outside the application folder and my file name is 123.jpg. So it should be like this:

    unlink("./uploads/123.jpg");
    
    0 讨论(0)
  • 2021-01-04 08:26
    $file = "test.txt";
    if (!unlink($file))
      {
      echo ("Error deleting $file");
      }
    else
      {
      echo ("Deleted $file");
      }
    
    0 讨论(0)
  • 2021-01-04 08:30

    you can use the "file helper" in codeigniter.

    CodeIgniter v3: http://codeigniter.com/userguide3/helpers/file_helper.html#delete_files

    CodeIgniter v4: http://codeigniter.com/user_guide/helpers/filesystem_helper.html#delete_files

    and like this :

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

    Late Edit: delete_filesmethod uses a path to wipe out all of its contents via unlink() and same you can do within CI. Like this:

    unlink($path); 
    

    a valid path.

    0 讨论(0)
提交回复
热议问题