/** * Creates a file * * @param string $job_id * @param string $filename * @param string $filetype * @return string * @throws \EventEspressoBatchRequest\Helpers\BatchRequestException */ public function create_file_from_job_with_name($job_id, $filename, $filetype = 'application/ms-excel') { $filepath = ''; try { $base_folder = $this->get_base_folder(); $success = $this->_file_helper->ensure_folder_exists_and_is_writable($base_folder . JobHandlerFile::temp_folder_name); if ($success) { $success = $this->_file_helper->ensure_folder_exists_and_is_writable($base_folder . JobHandlerFile::temp_folder_name . DS . $job_id); } if ($success) { $filepath = $base_folder . JobHandlerFile::temp_folder_name . DS . $job_id . DS . $filename; $success = $this->_file_helper->ensure_file_exists_and_is_writable($filepath); } //let's add the .htaccess file so safari will open the file properly if ($success) { $extension = \EEH_File::get_file_extension($filepath); \EEH_File::write_to_file($base_folder . JobHandlerFile::temp_folder_name . DS . $job_id . DS . '.htaccess', 'AddType ' . $filetype . ' ' . $extension, '.htaccess'); } //those methods normally fail with an exception, but if not, let's do it if (!$success) { throw new \EE_Error(__('Could not create temporary file, an unknown error occurred', 'event_espresso')); } } catch (\EE_Error $e) { throw new BatchRequestException(sprintf(__('Could not create temporary file for job %1$s, because: %2$s ', 'event_espresso'), $job_id, $e->getMessage()), 500, $e); } return $filepath; }
public function test_get_file_extension() { $file_path = '/var/whatever/thing.txt'; $this->assertEquals('txt', EEH_File::get_file_extension($file_path)); }
/** * get_file_contents * @param string $full_file_path * @return string */ public static function get_file_contents($full_file_path = '') { $full_file_path = EEH_File::standardise_directory_separators($full_file_path); if (EEH_File::verify_filepath_and_permissions($full_file_path, EEH_File::get_filename_from_filepath($full_file_path), EEH_File::get_file_extension($full_file_path))) { // load WP_Filesystem and set file permissions $wp_filesystem = EEH_File::_get_wp_filesystem(); return $wp_filesystem->get_contents($full_file_path); } return ''; }