function remove_upload($field_name, $index) { $field = $this->_get_single_field($field_name); $ids = \Leeflets\String::parse_array_representation($field_name); $data = $this->content->get_data(); $template_content = new \Leeflets\Template\Content($data); $files = $template_content->vget($ids); if ($field->has_multiple_values) { if (!isset($files[$index])) { echo json_encode(array('error' => 'File index not found.')); exit; } $file = $files[$index]; } else { $file = $files; } // If this is a sample image in the template, don't try remove the file if (!isset($file['in_template']) || !$file['in_template']) { // Try removing the files @unlink($this->config->uploads_path . '/' . $file['path']); if (isset($file['versions'])) { foreach ($file['versions'] as $version) { @unlink($this->config->uploads_path . '/' . $version['path']); } } // Check if any of the files are still there $all_removed = true; if (is_file($this->config->uploads_path . '/' . $file['path'])) { $all_removed = false; } elseif (isset($file['versions'])) { foreach ($file['versions'] as $version) { if (is_file($this->config->uploads_path . '/' . $version['path'])) { $all_removed = false; } } } } else { $all_removed = true; } if ($all_removed) { // Remove file from data and save if ($field->has_multiple_values) { unset($files[$index]); } else { $files = array(); } $files_array = \Leeflets\String::convert_representation_to_array($field_name, $files); $data = $this->content->get_data(); $_data =& $data; foreach ($ids as $id) { $data =& $data[$id]; } $data = $files; $this->content->set_data($_data); echo json_encode(array('success' => true)); } else { echo json_encode(array('error' => 'Some files could not be removed.')); } exit; }