delete the uploaded file

edited June 2015 in Modules
I have searched and browsed this forum and found no way to clear a field after uploaded, for example, a wrong file (i.e. to NULL the underlaid DB field); anyway to do it?

Comments

  • edited 4:29PM
    I mean a file input field
  • edited 4:29PM
    I'm not quite sure I understand what you are asking. What type of field are you talking about? Is it perhaps wanting to manipulate what gets saved? If so, have you looked at model hooks:
    http://docs.getfuelcms.com/general/models#hooks
  • edited 4:29PM
    My situation is that
    1. I have successfully uploaded a file, e.g. a pdf file, via the file input field (an optional field) <input type="file"> generated by the Fuel CMS, together with other text contents fields
    2. somehow after, I want to take away the file but want to keep other text contents to display on the website, I cannot find a way to remove the referenced file name / path from the DB field via the Fuel CMS generated form (but I know how to physically delete the file)
  • edited 4:29PM
    That sounds like you may want to use the hooks as suggested above. There is an on_after_post hook method you can use on your model to manipulate both the uploaded file (if you want) and the data.
    function on_after_post($values){ $record = $this->find_by_key($values['id']); $record->my_file_field = ''; // blank out the field $record->save(); // you can access the uploaded file information on the model with the upload_data property var_dump($this->upload_data); }
  • edited 4:29PM
    Actually, I know how to use the hook. But what I need is a form element e.g. a check box (labelled "delete file") for the user to interact on, so that when the user want to remove the file, she can check the box to signify the system to remove the file but retain other data after submit.
  • edited June 2015
    You can add the check box in your model's form_fields method. Then in your model hook look for that checkbox value in the post to signal the deletion:
    function on_after_save($values){ if ($this->input->post('delete_file')) { // delete file } }
  • edited 4:29PM
    Thanks for your previous reply; however, I've tried your suggestion to add a 'artificial' field like this to the form_fields method:

    $fields['cat1_shot_delete'] = array('type' => 'checkbox', 'label' => 'Delete image 1 ');

    but the on_after_save / on_before_save methods cannot receive the 'cat1_shot_delete' field value, checked both via var_dump and Chrome browser's debug panel; only the field values with database fields returned

    how come?
  • edited 4:29PM
    This is why the example above targeted the post. The $values array only includes values that will be saved. You can get the post data using the normalize_save_values method:
    function on_after_save($values){ $posted = $this->normalize_save_values(); if (!empty($posted['delete_file'])) { // delete file } }
Sign In or Register to comment.