File API

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.

Create a managed file


/**
 * Create a directory, add a managed file,
 * and create url from the new file object.
 */
function _demo_file_managed() {
 
  $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 a managed file.
  // Managed means a database entry is created in the file_managed table.
Subscribe to RSS - File API