Jonathan.
Jonathan.

Reputation: 55534

Delete a file on server from CodeIgniter view

I have a list of files that is retrieved in a Controller, the array is then passed to a view where it is displayed, each with a delete button.

Because the file is on the server it has to be done in PHP and therefore in the controller (it wouldn't not make sense (following MVC) to have it in the View). How do I tell the controller to perform the delete, while passing it the filename to delete.

MVC, to me, means:

M<-->C<-->V

Where the controller can talk to the view and vice versa. I'm not using Model so ignoring that it seem like CodeIgnite is just:

C--->V

Basically I want to perform a PHP method and a pass an arguement to it based on what button the user clicks in the view. And I do not want to leave the page. How do I do this?

Upvotes: 0

Views: 491

Answers (2)

Foyaz Ullah Shahin
Foyaz Ullah Shahin

Reputation: 424

You can Try this :

 $files = glob('/home/xxx/public_html/project_name/application/view/welcome.php');

 foreach($files as $file){ 

 if(is_file($file))

 unlink($file);

  }

Upvotes: 0

user973254
user973254

Reputation:

Use ajax javascript method which will send request to proper controller which will delete your file.

Using jQuery it will be looking like:

<script type="text/javascript">
function deleteSomething(id) {
    $.ajax({
      url: 'controller/delete_method',
      type: 'POST',
      success: function(data) {
        alert(id + ' was removed.');
      }
    });
}
</script>

<input type="button" value="delete" onClick="deleteSomething(1);" />

Upvotes: 1

Related Questions