[development] creating a file - path problems

Jennifer Hodgdon yahgrp at poplarware.com
Fri Nov 6 17:01:29 UTC 2009


Jeff Greenberg wrote:
> When coding
> 
> realpath(file_directory_path()) . 'the/desired/filename.ext';
> 
> Isn't there an assumption that file_directory_path() is relative to the 
> current path? Otherwise how does realpath know where it's intended to 
> go? Which kind of brings me back to the original question, of 
> file_directory_path being relative to...what? Will my current position, 
> when invoking file_directory_path() always be relative to the Drupal 
> root, even if the current page has a path that is a subdirectory off the 
> Drupal root?

file_directory_path() is relative to the Drupal root. PHP's realpath() 
function is relative to the "current" directory. If you have not run 
chdir() anywhere, it appears that PHP sets the current directory to 
the directory of the original PHP file (I think?), which would be the 
index.php file of Drupal, which is also in the Drupal root. So it does 
work, somewhat magically.

Incidentally, I'll just mention that the previous suggestion of
    $_SERVER['DOCUMENT_ROOT'] . $base_path . (whatever)
is not going to work in all situations, since depending on the Apache 
config, the directory for some web spaces may not even be under the 
server's document root. For instance, on my test box the doc root is 
/var/www/, but I have Apache configured so that the URL 
http://localhost/~xyz maps to /opt/www/xyz.

realpath(), on the other hand, works perfectly in this case. You can 
also do something like
     realpath( drupal_get_path('module', 'mymodulename'))
if you need to reference a file in your module directory for some 
reason (e.g. in a test).

     --Jennifer

-- 
Jennifer Hodgdon * Poplar ProductivityWare
www.poplarware.com
Drupal, WordPress, and custom Web programming



More information about the development mailing list