Create an unmanaged file


/**
 * Create a directory, add an unmanaged file
 * and create url from the new filepath.
 */
function _demo_file_unmanaged() {
  
  $filedir = 'public://demo';
 
  // Create a demo directory in the public dir.
  // file_prepare_directory checks, creates and sets permissions on a directory path.
  $prepared = file_prepare_directory($filedir, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY); 
  dpm('Directory prepared => ' . $prepared);
  
  // Save an unmanaged file.
  // Unmanaged means nothing is saved into the database.
  $filepath = file_unmanaged_save_data('This is a demo text.', $filedir . '/demo.txt');
  dpm($filepath);
  
  // Create an url from a filepath.
  $url = file_create_url($filepath);
  dpm($url);
  
}

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.