[development] creating a file - path problems

andrew morton drewish at katherinehouse.com
Fri Nov 6 14:09:43 UTC 2009


It's a bit of a pain to learn but you'll save yourself some headaches
by using Drupal's file functions in includes/file.inc for doing path
handling. If you're trying to create a file in Drupal's files
directory use:
  file_create_path('the/desired/filename.ext');
which give you a path relative to Drupal's root. If you need the full
path from the server root you'll need to use PHP's realpath() which
only works with existing files so you'll need to use:
  realpath(file_directory_path()) . 'the/desired/filename.ext';
Once you get some data saved in there you can use file_create_url() to
get a URL for the file that you can send out to visitors so they can
download the file.

andrew

On Fri, Nov 6, 2009 at 12:57 AM, Jeff Greenberg <jeff at ayendesigns.com> wrote:
> Actually, it's worse...I forgot $base_path (which makes a difference if
> you're drupal root is a subdirectory)... so it's
>
> $fn = $_SERVER['DOCUMENT_ROOT'] . $base_path . $file_document_path() .
> '/myfilename';
>


More information about the development mailing list