sys_get_temp_dir in shared hosting environment

后端 未结 7 1514
日久生厌
日久生厌 2021-01-31 10:27

Note: This could also fit in superuser.

I am setting up PHP 5.3.10 on a shared host with apache2 mpm itk and open_basedir in a way, that each user may not see o

相关标签:
7条回答
  • 2021-01-31 10:50

    Looking at the PHP source, sys_get_temp_dir() works with the following priority:

    1. If its value has been calculated before, the cached value is used.
    2. sys_temp_dir is checked in the ini configuration.
    3. On Windows, the GetTempPathW Win32 API method is used, and according to its documentation the following are used (in this order):
      1. The path specified by the TMP environment variable.
      2. The path specified by the TEMP environment variable.
      3. The path specified by the USERPROFILE environment variable.
      4. The Windows directory.
    4. In *nix, the following are used (in this order):
      1. The TMPDIR environment variable.
      2. The P_tmpdir macro
      3. /tmp (according to the source, this is a last-ditch effort that should never happen).

    That should give you enough options for controlling the result of sys_get_temp_dir (e.g. ini_set('sys_temp_dir', $tmpPath) or putenv('TMPDIR=/foo/bar') as others mentioned) Unless it was previously calculated, in which case you're SOL as far as I know and the cached value will be used (but I have zero knowledge in PHP so would love to hear otherwise).

    0 讨论(0)
  • 2021-01-31 10:55

    In case people end up here whos Problem is not solved with putenv...

    ... for me, it worked to set the sys_temp_dir using php's ini_set like this:

    $tmpPath = realpath(__DIR__.'/../app/tmp');
    ini_set('sys_temp_dir', $tmpPath);
    

    I am running PHP 5.5.9 (cli) on a windows8 machine.

    0 讨论(0)
  • 2021-01-31 10:58

    According to this - 4 year old - bug, sys_get_temp_dir() won't work with virtual-hosts; so

    • you can try to use only libraries that fixed this issue (& open a bug for those who didn't)
    • or append /tmp (or whatever your OS uses) in your open_basedir, as it can hold multiple directories (like include_path - separate it with ; on Windows, : otherwise)
    0 讨论(0)
  • 2021-01-31 11:00

    Running a putenv('TMPDIR=/foo/bar') inside PHP seems to be able to affect the result of sys_get_temp_dir(). You could have an auto_prepend_file directive arranged to run a piece of PHP to set up the TMPDIR and avoid messing with a redefinition of sys_get_temp_dir().

    Edit: Also, you could easily use putenv('TMPDIR='.ini_get('open_basedir').'/tmp') to set the temporary directory to the directory structure you laid out in the question.

    Funny enough, this turns out to also work (given that you keep the SetEnv TMPDIR /foo/bar in your Apache configuration):

    putenv('TMPDIR='.getenv('TMPDIR'));
    

    Seems like a no-op, but actually does have effect on sys_get_temp_dir(). I'm starting to suspect this has to be some environment-handling bug in PHP.

    0 讨论(0)
  • 2021-01-31 11:05

    You have tagged your question cgi, however you are making use of

     php_admin_value  open_basedir      /home/userA/www/
     ^^^^^^^^^^^^^^^
    

    which is a setting for the apache module version of PHP, Mod_PHP. In that case PHP is loaded once the webserver starts.

    Then you make use of SetEnv:

     SetEnv           TMPDIR            /home/userA/www/tmp/
    

    this is setting an internal environment variable. It is passed to other apache modules, however I think you need it with the request, not with the virtual server. I don't know it specifically, but I would assume according to your description that this environment variable is getting reset before the PHP script is invoked.

    So more a comment than a real answer, but hopefully it helps you clarify some things.

    I normally use FCGI for multi-user environments so that I can better separate the users. I never had problems with setting environment variables per each user. But that's just another comment, I don't want to say you have to use it, too. Just to highlight that you need to find out the right place within apache to set the environment variable so it is (still) set when the script is executed.


    Also you might not be setting the right environment variable. According to Apache Documentation about environment variables:

    Although these variables are referred to as environment variables, they are not the same as the environment variables controlled by the underlying operating system. Instead, these variables are stored and manipulated in an internal Apache structure. They only become actual operating system environment variables when they are provided to CGI scripts and Server Side Include scripts. If you wish to manipulate the operating system environment under which the server itself runs, you must use the standard environment manipulation mechanisms provided by your operating system shell.

    You want to set the operating system environment variable for PHP. But you are setting the internal environment variable only.

    Mod_PHP might import them to the script, so if you use getenv('TMPDIR') the PHP SAPI specific implementation is used - which does allow you to see those internal environment variables - however the php_get_temporary_directory function is not using it - it looks like.

    Please add your Apache and PHP version to your question.

    0 讨论(0)
  • 2021-01-31 11:08

    This is a bug in php 5.2 - specify temp dir by php.ini
    It's fixed in 5.5
    Use this as a temporary solution:

    <?php
    putenv('TMPDIR=/path/to/your/tmp');
              ...your code here ...
    
    0 讨论(0)
提交回复
热议问题