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.


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

                'file_extensions' => '',
                'file_directory' => $type,
                'uri' => 'public://'.$type,

            'widget' => array(
                    'type' => 'file_generic',


I want to have files for content type is seperate directory in sites/default/files.

1 Answer 1


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' => $dir), WATCHDOG_ERROR);
   return $res;


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

  • It works for me, thanks. I've just need to change bit operator from | to &.
    – Codium
    Commented Aug 17, 2012 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.