file_check_path

5 file.inc file_check_path(&$path)
6 file.inc file_check_path(&$path)

Checks path to see if it is a directory, or a dir/file.

Parameters

$path A string containing a file path. This will be set to the: directory's path.

Return value

If the directory is not in a Drupal writable directory, FALSE is returned. Otherwise, the base name of the path is returned.

Related topics

2 calls to file_check_path()

File

includes/file.inc, line 161
API for handling file uploads and server file management.

Code

function file_check_path(&$path) {
  // Check if path is a directory.
  if (file_check_directory($path)) {
    return '';
  }

  // Check if path is a possible dir/file.
  $filename = basename($path);
  $path = dirname($path);
  if (file_check_directory($path)) {
    return $filename;
  }

  return FALSE;
}

Comments

Be careful with how you use this function...

The help text for this function doesn't mention that $path is taken by reference. Thus, when using an IDE that shows helptext for code-completion, it isnt clear that when using this function to "check a path", the given path with a file name will be rewritten to only include the path, and the file name will be returned separately.

In general, this means that writing something like the following will have unexpected results:

$file_with_path="mydir/file.txt";

if(file_check_path($file_with_path)){
  file_save_data("test content",$file_with_path); 
}

when that runs, the file that gets saved will be in the right folder, but will have a tempfile name instead of the actual name because the file name is stripped out.

Good admonishment

Ha! Thanks for pointing this out, netw3rker. It's a curious interface behaviour and I wonder what the use case(s) could be. I don't expect a function whose verb is "check" to change anything. Any suggestions?

Login or register to post comments