How to specify subdirectory for uploads in files dir when adding file field to content type via module? This question is related to: https://drupal.stackexchange.com/questions/39884/how-to-create-file-field-instance-based-on-file-field-info-in-custom-module

If I don't specify directory I can upload file.

However if I try do it I am getting error:


  The upload directory public://osw_majatkowe_m for the file field
  field_osw_majatkowe_m_file could not be created or is not accessible.
  A newly uploaded file could not be saved in this directory as a
  consequence, and the upload was canceled.


osw_majatkowe_m is my content type stored in $type variable.

Code:

    $instance = array(
            'field_name' =&gt; $ct_info['file_field_name'],
            'entity_type' =&gt; 'node',
            'label' =&gt; 'Plik',
            'bundle' =&gt; $type,
            'required' =&gt; false,
            'settings' =&gt; array(

                'file_extensions' =&gt; '',
                'file_directory' =&gt; $type,
                'uri' =&gt; 'public://'.$type,

            ),
            'widget' =&gt; array(
                    'type' =&gt; 'file_generic',
            ),
    );

    field_create_instance($instance);


I want to have files for content type is seperate directory in sites/default/files.
It seems to me that your directory does not exists (or at least is not writable).
You can make a function to test/create your custom folder:


function create_directory($folder) {
   // Constructs a URI to Drupal's default files location given a relative path
   $dir = file_build_uri($folder);
   // Checks that the directory exists and is writable.
   // Indicate if the directory should be created if it does not exist (FILE_CREATE_DIRECTORY) or made writable if it is read-only (FILE_MODIFY_PERMISSIONS)
   $res = file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
   if (!$res) {
     watchdog('file system', 'The directory %directory does not exist or is not writable.', array('%directory' =&gt; $dir), WATCHDOG_ERROR);
   }
   return $res;

}




Then call this function with $type as parameter: create_directory($type);