You are here

function views_data_export_plugin_display_export::file_save_file in Views data export 6

Same name and namespace in other branches
  1. 6.3 plugins/views_data_export_plugin_display_export.inc \views_data_export_plugin_display_export::file_save_file()
  2. 6.2 plugins/views_data_export_plugin_display_export.inc \views_data_export_plugin_display_export::file_save_file()
  3. 7.4 plugins/views_data_export_plugin_display_export.inc \views_data_export_plugin_display_export::file_save_file()
  4. 7 plugins/views_data_export_plugin_display_export.inc \views_data_export_plugin_display_export::file_save_file()
  5. 7.3 plugins/views_data_export_plugin_display_export.inc \views_data_export_plugin_display_export::file_save_file()

Save a file into a file node after running all the associated validators.

This function is usually used to move a file from the temporary file directory to a permanent location. It may be used by import scripts or other modules that want to save an existing file into the database.

Parameters

$filepath: The local file path of the file to be saved.

$account: The user account object that should associated with the uploaded file.

Return value

An array containing the file information, or 0 in the event of an error.

1 call to views_data_export_plugin_display_export::file_save_file()
views_data_export_plugin_display_export::outputfile_create in plugins/views_data_export_plugin_display_export.inc
Called on export initialization Creates the output file, registers it as a temporary file with Drupal and returns the fid

File

plugins/views_data_export_plugin_display_export.inc, line 723
Contains the bulk export display plugin.

Class

views_data_export_plugin_display_export
The plugin that batches its rendering.

Code

function file_save_file($filepath, $account = NULL) {
  if (!isset($account)) {
    $account = $GLOBALS['user'];
  }

  // Begin building file object.
  $file = new stdClass();
  $file->uid = $account->uid;
  $file->filename = basename($filepath);
  $file->filepath = $filepath;
  $file->filemime = module_exists('mimedetect') ? mimedetect_mime($file) : file_get_mimetype($file->filename);

  // Rename potentially executable files, to help prevent exploits.
  if (preg_match('/\\.(php|pl|py|cgi|asp|js)$/i', $file->filename) && substr($file->filename, -4) != '.txt') {
    $file->filemime = 'text/plain';
    $file->filepath .= '.txt';
    $file->filename .= '.txt';
  }
  $file->filesize = filesize($filepath);

  // If we made it this far it's safe to record this file in the database.
  $file->status = FILE_STATUS_TEMPORARY;
  $file->timestamp = time();

  // Insert new record to the database.
  drupal_write_record('files', $file);
  return (object) $file;
}