I have three files which when i click on delete should be removed. How am I able to use the unlink and realpath for my multiple files?

public function delete() {
$path_to_file =  FCPATH . 'application/modules/admin/controllers/module' .'/'. $this->uri->segment(5).'.php';
$path_to_file_lang =  FCPATH . 'application/modules/admin/language'.'/' .'english' .'/'. 'module/'.   $this->uri->segment(5).'lang.php';
$path_to_file_view =  FCPATH . 'application/modules/admin/views/template/module' .'/'. $this->uri->segment(5).'.tpl';

if (unlink(realpath($path_to_file), realpath($path_to_file_lang))  ) {

echo "deleted" .' '. $path_to_file;

echo "<br/>";

echo "deleted" .' '. $path_to_file_lang;

echo "<br/>";

echo "deleted" .' '. $path_to_file_view;

} else {

echo "did not delete";

}
}

I have found my own solution deleting multiple selected files to me a while but here it is

public function delete() {
        $path[] =  FCPATH . 'application/modules/admin/controllers/module' .'/'. $this->uri->segment(5).'.php';
        $path[] =  FCPATH . 'application/modules/admin/language'.'/' .'english' .'/'. 'module/'. $this->uri->segment(5).'_lang.php';
        $path[] =  FCPATH . 'application/modules/admin/views/template/module' .'/'. $this->uri->segment(5).'.tpl';

        foreach ($path as $file) {

        if (unlink(realpath($file))) {
            echo "deleted file" .' '. "<br/><br/>"$file;
        } else {
            echo "did not delete" .' '. $file;
        }

        }
    }

I would setup my delete function to pass an array of the files to the function that I wanted to delete, and then loop through the array in the function.

@pixelsoul do you have a example. All ways willing to try new and better ways.

Here is an example of how I would probably start. I would probably be adding a little more to this, but it's a starting point. I tend to keep things pretty flexible so I can use it in another project with minimal/no changes needed.

$files = array("files/5.txt", "files/6.txt", "files/9.txt", "files/11.txt");

function delete($array){

    $results = array(
        "success" => array(), 
        "failed" => array(), 
        "not_found" => array()
    );

    $i = 0;
    foreach($array as $key => $name)
    {
        $file = realpath($name);

        if(file_exists($file))
        {
            if(unlink($file))
            {
                $results["success"][] = $file;
            }
            else
            {
                $results["failed"][] = $file;
            }
        }
        else
        {
            $results["not_found"][] = $name;
        }
        $i++;
    }

    return $results;
}

var_dump(delete($files));

With my function I would get back an array with the results, so I can see exactly what happened with each file it processed. With the information in the results array I can add some more logic for reporting that to the frontend or what step to take next.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.