PHP file_put_contents with absolute path?

12,916

Solution 1

realpath() will tell you if it is a valid path in the filesystem. To test if the path is actually in a given 'sandbox' path, see the answers to this question I asked here recently.

Solution 2

How about something like:

file_put_contents($_SERVER['DOCUMENT_ROOT']."path/to/user/wordpress/account/".TEMPLATEPATH.'/ps_logo2.png');

edit

$_SERVER['SCRIPT_NAME'] gives the path to the script currently being processed.

Share:
12,916

Related videos on Youtube

Jens Törnell
Author by

Jens Törnell

Updated on June 04, 2022

Comments

  • Jens Törnell
    Jens Törnell almost 2 years

    I use Wordpress and PHP and the function file_put_contents(). Wordpress file structure can look different depending on the user.

    I need to call it like this:

    file_put_contents(TEMPLATEPATH . '/ps_logo2.png');
    

    or the same thing like this:

    file_put_contents('C:\wamp\www\domain\modehallen.se/wp-content/uploads/images/ps_logo2.png');
    

    The case above is the absolute path on localhost (that's why it's C:). I need the path or some other way to make sure the file is put in the right place.

    How is this done?

  • Decent Dabbler
    Decent Dabbler about 13 years
    Or did I misinterpret your question?
  • Jens Törnell
    Jens Törnell about 13 years
    It helped me solve the problem. It returned nothing at first and that helped me find out that the folder to put the contents in was not created. It needs to be created first.
  • Decent Dabbler
    Decent Dabbler about 13 years
    @Jens: ok, you may also want to look at file_exists() which actually is intended for seeing if a file/dir exists. realpath() is a bit of a shortcut that expands paths to their real path equivalent, and returns false on failure, which most of the time would mean the path does not exist. I thought you might wanna know that too.

Related