function field_file_check_directory in FileField 6.3
Same name and namespace in other branches
- 6.2 field_file.inc \field_file_check_directory()
A silent version of file.inc's file_check_directory().
This function differs from file_check_directory in that it checks for files when doing the directory check and it does not use drupal_set_message() when creating directories. This function may be removed in Drupal 7.
Check that the directory exists and is writable. Directories need to have execute permissions to be considered a directory by FTP servers, etc.
Parameters
$directory A string containing the name of a directory path.:
$mode A Boolean value to indicate if the directory should be created: if it does not exist or made writable if it is read-only.
$form_item An optional string containing the name of a form item that: any errors will be attached to. This is useful for settings forms that require the user to specify a writable directory. If it can't be made to work, a form error will be set preventing them from saving the settings.
Return value
FALSE when directory not found, or TRUE when directory exists.
1 call to field_file_check_directory()
- filefield_save_upload in ./
filefield_widget.inc - Given a FAPI element, save any files that may have been uploaded into it.
File
- ./
field_file.inc, line 320 - Common functionality for file handling CCK field modules.
Code
function field_file_check_directory(&$directory, $mode = 0, $form_item = NULL) {
$directory = rtrim($directory, '/\\');
// Error if the directory is a file.
if (is_file($directory)) {
watchdog('file system', 'The path %directory was checked as a directory, but it is a file.', array(
'%directory' => $directory,
), WATCHDOG_ERROR);
if ($form_item) {
form_set_error($form_item, t('The directory %directory is a file and cannot be overwritten.', array(
'%directory' => $directory,
)));
}
return FALSE;
}
// Create the directory if it is missing.
if (!is_dir($directory) && $mode & FILE_CREATE_DIRECTORY && !@mkdir($directory, 0775, TRUE)) {
watchdog('file system', 'The directory %directory does not exist.', array(
'%directory' => $directory,
), WATCHDOG_ERROR);
if ($form_item) {
form_set_error($form_item, t('The directory %directory does not exist.', array(
'%directory' => $directory,
)));
}
return FALSE;
}
// Check to see if the directory is writable.
if (!is_writable($directory) && $mode & FILE_MODIFY_PERMISSIONS && !@chmod($directory, 0775)) {
watchdog('file system', 'The directory %directory is not writable, because it does not have the correct permissions set.', array(
'%directory' => $directory,
), WATCHDOG_ERROR);
if ($form_item) {
form_set_error($form_item, t('The directory %directory is not writable', array(
'%directory' => $directory,
)));
}
return FALSE;
}
if ((file_directory_path() == $directory || file_directory_temp() == $directory) && !is_file("{$directory}/.htaccess")) {
$htaccess_lines = "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\nOptions None\nOptions +FollowSymLinks";
if (($fp = fopen("{$directory}/.htaccess", 'w')) && fputs($fp, $htaccess_lines)) {
fclose($fp);
chmod($directory . '/.htaccess', 0664);
}
else {
$repl = array(
'%directory' => $directory,
'!htaccess' => nl2br(check_plain($htaccess_lines)),
);
form_set_error($form_item, t("Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines:<br /><code>!htaccess</code>", $repl));
watchdog('security', "Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines:<br /><code>!htaccess</code>", $repl, WATCHDOG_ERROR);
}
}
return TRUE;
}