Drupal 7 – Starting point for creating a file object programatically

If you need to create a file object through the Drupal api then use the following code:


filename = basename('{CURRENT PATH}');
$file->filepath = '{CURRENT PATH}';
$file->filemime = file_get_mimetype($file->filename);
$file->filesize = filesize('{CURRENT PATH}');
$file->uid = $user->uid;
$file->status = FILE_STATUS_TEMPORARY;
$file->timestamp = time();
$file->list = 1;
$file = file_save($file);
$file = file_move($file, 'public://');
$file->status = FILE_STATUS_PERMANENT;
$file = file_save($file);

Leave a Reply