| 5 file.inc | file_check_directory(&$directory, $mode = 0, $form_item = NULL) |
| 6 file.inc | file_check_directory(&$directory, $mode = 0, $form_item = NULL) |
Checks whether a directory exists and is writable.
Furthermore, the directory can optionally be created if it does not exist, and/or be set to writable if it is currently not. Directories need to have execute permission to be considered a directory by FTP servers.
Parameters
$directory: A string representing the directory path.
$mode: An optional bitmask containing the actions, if any, to be carried out on the directory. Any combination of the actions FILE_CREATE_DIRECTORY and FILE_MODIFY_PERMISSIONS is allowed.
$form_item: An optional string containing the name of a form item that any errors will be attached to. Useful when the function validates a directory path entered as a form value. An error will consequently prevent form submit handlers from running, and instead display the form along with the error messages.
Return value
FALSE if the directory does not exist or is not writable, even after any optional actions have been carried out. Otherwise, TRUE is returned.
Related topics
File
- includes/
file.inc, line 108 - API for handling file uploads and server file management.
Code
<?php
function file_check_directory(&$directory, $mode = 0, $form_item = NULL) {
$directory = rtrim($directory, '/\\');
// Check if directory exists.
if (!is_dir($directory)) {
if (($mode & FILE_CREATE_DIRECTORY) && @mkdir($directory)) {
drupal_set_message(t('The directory %directory has been created.', array('%directory' => $directory)));
@chmod($directory, 0775); // Necessary for non-webserver users.
}
else {
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)) {
if (($mode & FILE_MODIFY_PERMISSIONS) && @chmod($directory, 0775)) {
drupal_set_message(t('The permissions of directory %directory have been changed to make it writable.', array('%directory' => $directory)));
}
else {
form_set_error($form_item, t('The directory %directory is not writable', array('%directory' => $directory)));
watchdog('file system', 'The directory %directory is not writable, because it does not have the correct permissions set.', array('%directory' => $directory), WATCHDOG_ERROR);
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 {
$variables = array(
'%directory' => $directory,
'!htaccess' => '<br />' . 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: <code>!htaccess</code>", $variables));
watchdog('security', "Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines: <code>!htaccess</code>", $variables, WATCHDOG_ERROR);
}
}
return TRUE;
}
?> Login or register to post comments
Comments
In Drupal 7, now file_prepare_directory()
This function has been renamed in Drupal 7: http://api.drupal.org/api/function/file_prepare_directory/7
Here is a real world Example
Here is a little real world example of how to use the "file_check_directory()" function.
I use this example function in many of my projects. It does more or less what the new "file_move()" function for Drupal 7 does.
You can call this function as follows:
move_file(555, 'sites/default/files/image1.jpg', 'sites/default/files/JPG/image1.jpg');
Where '555' is the fid, the first path is the original file path, and the second path is the destination path.
The function is pretty well commented.
<?php
/*
This function will:
1) Create a folder if not existent
2) Move the file into the new location
3) Update the file record on the database
$file_id => Is the fid that you want to move.
$org_path => Is the full original file path including directory tree and file name.
$new_path => Is the full destination file path including directory tree and file name.
*/
function move_file($file_id, $org_path, $new_path){
//Get just the directory path from the full file path.
$new_dir_path = substr($new_path, 0, strrpos($new_path, '/'));
//If new directory path does not exist, create it.
file_check_directory($new_dir_path, $mode = FILE_CREATE_DIRECTORY);
//Try to move the file into the new location.
$success = file_move($org_path, $new_path, $replace = FILE_EXISTS_REPLACE);
//If the file move was successful, update the database record.
if ($success){
$result = db_query('UPDATE {files} SET filepath = "%s" WHERE fid = %d', $new_path, $file_id);
}
}
?>
correction
Thanks for the example. There are a couple mistakes though.
//If new directory path does not exist, create it.
file_check_directory($new_dir_path, $mode = FILE_CREATE_DIRECTORY);
//Try to move the file into the new location.
$success = file_move($org_path, $new_path, $replace = FILE_EXISTS_REPLACE);
should be
//If new directory path does not exist, create it.
file_check_directory($new_dir_path, FILE_CREATE_DIRECTORY);
//Try to move the file into the new location.
$success = file_move($org_path, $new_path, FILE_EXISTS_REPLACE);
the variable declarations are removed from the function calls.
aaron
You are absolutely correct MraaronCruz!
You are absolutely correct MraaronCruz! I am sorry for this lack of attention on my part. Here is my example with the corrections suggested by MraaronCruz.
<?php
/**
* @desc This function will:
* 1) Create a folder if not existent
* 2) Move the file into the new location
* 3) Update the file record on the database
*
* @param $file_id (Integer) Required -> Is the fid that you want to
* move.
* @param $org_path (String) Required -> Is the full original file path
* including directory tree and file name.
* @param $new_path (String) Required -> Is the full destination file
* path including directory tree and file name.
*/
function move_file($file_id, $org_path, $new_path){
//Get just the directory path from the full file path.
$new_dir_path = substr($new_path, 0, strrpos($new_path, '/'));
//If new directory path does not exist, create it.
file_check_directory($new_dir_path, FILE_CREATE_DIRECTORY);
//Try to move the file into the new location.
$success = file_move($org_path, $new_path, FILE_EXISTS_REPLACE);
//If the file move was successful, update the database record.
if ($success){
db_query('UPDATE {files} SET filepath = "%s" WHERE fid = %d', $new_path, $file_id);
}
}
?>
!is_file("$directory/.htacces
!is_file("$directory/.htaccess")
How is this supposed to work? Are we really looking for file "$directory/.htaccess" or should we have "$directory" . "/.htaccess".
Or maybe I need a lesson in php?
Variables in double-quotes
Variables in double-quotes are expanded to their value. So:
<?php$test = 'blah';
print 'Testing $test'; // Prints: Testing $test
print "Testing $test"; // Prints: Testing blah
?>
In addition, the slash character is not a valid in a PHP variable (and thus marks the end of a variable name is a string of characters) so
"$directory/.htaccess"and"$directory" . "/.htaccess"and$directory . '/.htaccess'are all equivalent.In Drupal 7 use..
In Drupal7 use:
file_prepare_directory()
http://api.drupal.org/api/drupal/includes--file.inc/function/file_prepar...