15

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.

I would like to use the unlink() function in php but I really can't understand how to use it.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jetoox
  • 1,387
  • 6
  • 17
  • 36

9 Answers9

36

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.

Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
  • 5
    `delete_files($path)` requires a directory path and not a file path. I am not sure if that would do the job. – Aniket Dec 30 '12 at 09:35
  • 3
    Yes you're right. I looked it now and `delete_files` function uses a recursive `unlink()` to delete files in a specified folder. This question's answer should be updated. – Taha Paksu Dec 30 '12 at 11:55
  • the link in the answer is dead. – Blip Jul 17 '20 at 13:50
13

http://php.net/manual/en/function.unlink.php

It is the best way to understand. Read it!

$path_to_file = '/project/folder/file_to_delete';
if(unlink($path_to_file)) {
     echo 'deleted successfully';
}
else {
     echo 'errors occured';
}
AgentP
  • 6,261
  • 2
  • 31
  • 52
Pave
  • 2,347
  • 4
  • 21
  • 23
  • it gives an error ; unlink(localhost/project/files_to_delete/f1.php) [function.unlink]: No such file or directory – Jetoox Mar 17 '12 at 06:46
  • 1
    if it gives you an error, you should silence it like this `@unlink($path_to_file)` But to make sure the file exists, use [is_file function](http://php.net/manual/en/function.is-file.php) before deleting it. – machineaddict Mar 14 '13 at 14:34
6

to delete file use

unlink($file_name);

or to delete directory use

rmdir($dir);
rafi
  • 1,493
  • 3
  • 31
  • 43
4

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");
Subkhan Sarif
  • 459
  • 9
  • 20
  • Can I know why it should use ./ ? Because the `upload` folder is not inside the `application` folder? Shouldn't it must use only / instead? – adrianriyadi Jun 12 '19 at 11:15
3

Use FCPATH in unlink. You can try as follows this works for me:

$file_name = $SBLN_ROLL_NO."_ssc";
$file_ext = pathinfo($_FILES['ASSIGNMENT_FILE']['name'],PATHINFO_EXTENSION);

//File upload configuration
$config['upload_path'] = $upload_path;
$config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';
$config['file_name'] = $file_name.'.'.$file_ext;

//First save the previous path for unlink before update
$temp = $this->utilities->findByAttribute('SKILL_DEV_ELEMENT', array('APPLICANT_ID'=>$STUDENT_PERSONAL_INFO->APPLICANT_ID, 'SD_ID'=>$SD_ID));

//Now Unlink
if(file_exists($upload_path.'/'.$temp->ELEMENT_URL))
{
    unlink(FCPATH . $upload_path.'/'.$temp->ELEMENT_URL);
}

//Then upload a new file
if($this->upload->do_upload('file'))
{
    // Uploaded file data
    $fileData = $this->upload->data();
    $file_name = $fileData['file_name'];
}
Bablu Ahmed
  • 4,412
  • 5
  • 49
  • 64
2

simply can use:

$file = "uploads/my_test_file.txt";

if (is_readable($file) && unlink($file)) {
    echo "The file has been deleted";
} else {
    echo "The file was not found";
}
bharat
  • 1,762
  • 1
  • 24
  • 32
2
$file = "test.txt";
if (!unlink($file))
  {
  echo ("Error deleting $file");
  }
else
  {
  echo ("Deleted $file");
  }
Hkachhia
  • 4,463
  • 6
  • 41
  • 76
0

September 2018 this solution worked for me.

if(unlink(FCPATH . 'uploads/'.$filename)){
    echo "Deleted";
}else{
    echo "Found some error";
}
Amir Ur Rehman
  • 649
  • 1
  • 9
  • 28
0

This code can also handle non-empty folders - just use it in a helper.

if (!function_exists('deleteDirectory')) {
    function deleteDirectory($dir) {
    if (!file_exists($dir)) return true;
    if (!is_dir($dir) || is_link($dir)) return unlink($dir);
        foreach (scandir($dir) as $item) {
            if ($item == '.' || $item == '..') continue;
            if (!deleteDirectory($dir . "/" . $item)) {
                chmod($dir . "/" . $item, 0777);
                if (!deleteDirectory($dir . "/" . $item)) return false;
            };
        }
        return rmdir($dir);
    }
}
qwertzman
  • 784
  • 1
  • 9
  • 23