creating a file - path problems

View: New views
7 Messages — Rating Filter:   Alert me  

creating a file - path problems

by Jeff Greenberg-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I've run into a problem that I can't resolve with the docs because
there's no usage examples (I know, I know...but I can't write it if I
don't know it).


D6. I need to create a file. Seems simple. But I can't determine what
combination of variables give me a good path. $file_directory_path gives
me  sites/default/files  which is great if I have something to append it
to, such as /users/myusername/public_html/mydomain.com/drupal   but in
looking at the descriptions of the various file api functions/vars, I
don't seem to see one that does that as opposed to checking this,
verifying that, etc.


Re: creating a file - path problems

by Lee Rowlands :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, 05 Nov 2009 22:06:55 -0500, Jeff Greenberg <jeff@...>
wrote:
> me  sites/default/files  which is great if I have something to append it
> to, such as /users/myusername/public_html/mydomain.com/drupal   but in
> looking at the descriptions of the various file api functions/vars, I
> don't seem to see one that does that as opposed to checking this,
> verifying that, etc.
The PHP global variable $_SERVER['DOCUMENT_ROOT'] should tell you where
your site lives in the file system for Apache.

Re: creating a file - path problems

by Jeff Greenberg-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Lee Rowlands wrote:

> The PHP global variable $_SERVER['DOCUMENT_ROOT'] should tell you where
> your site lives in the file system for Apache.
>
>
>  
Thanks, Lee. Yes, that's what I did. It seems, though, that there should
be a Drupal function or global to avoid having:
$fn = $_SERVER['DOCUMENT_ROOT'] . '/' . $file_directory_path() .
'/myfilename';

Re: creating a file - path problems

by Jeff Greenberg-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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';

Re: creating a file - path problems

by andrew morton-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...> 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';
>

Re: creating a file - path problems

by Jeff Greenberg-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

andrew morton wrote:

> 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
>
>
>  
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?

Re: creating a file - path problems

by Jennifer Hodgdon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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