API page: http://api.drupal.org/api/drupal/includes%21file.inc/function/file_prepa...

Enter a descriptive title (above) relating to file_prepare_directory, then describe the problem you have found:

I don't think this function works with Drupal URIs (although I might be missing something). If you pass in "public://info/55", it should create files/info/55 and then succeed. The is_dir() returns false, so it runs the drupal_mkdir() part, so creates the directories, but then returns false.

The $directory variable taken as input can be either a URI or a pathname. However, if it's a URI, it is never converted to an actual path, so the is_dir() check will always fail for URIs. If you pass it an actual filesystem path, it works correctly.

I haven't tested this, but I think the code should be something like:

function file_prepare_directory(&$directory, $options = FILE_MODIFY_PERMISSIONS) {
  $real_path = $directory;
  if (!file_stream_wrapper_valid_scheme(file_uri_scheme($directory))) {
    // Only trim if we're not dealing with a stream.
    $directory = rtrim($directory, '/\\');
  } else {
    $real_path = drupal_realpath($directory);
  }

  // Check if directory exists.
  if (!is_dir($real_path)) {
    // Let mkdir() recursively create directories and use the default directory
    // permissions.
    if (($options & FILE_CREATE_DIRECTORY) && @drupal_mkdir($directory, NULL, TRUE)) {
      return drupal_chmod($directory);
    }
    return FALSE;
  }
  // The directory exists, so check to see if it is writable.
  $writable = is_writable($directory);
  if (!$writable && ($options & FILE_MODIFY_PERMISSIONS)) {
    return drupal_chmod($directory);
  }

  return $writable;
}

Note: drupal_realpath() is apparently deprecated, although I'm not sure what is supposed to replace it.

Comments

rob_johnston’s picture

Status: Active » Needs review
StatusFileSize
new518 bytes

I found that when using IIS on Windows it was the is_writable() function that returned neither true nor false. I've tested the attached patch on both IIS7 on Windows 7, IIS6 on Windows 2003, and Apache on Ubuntu (although this one didn't need a patch to work correctly).

This can easily be tested by choosing "Add file" from the /admin/content/file page.

gmclelland’s picture

Issue summary: View changes
Related issues: +#2102905: Passing a uri to is_writable

Adding similar issue

dooug’s picture

Status: Needs review » Closed (duplicate)