title: dynamically set upload_tmp_dir in apache2
body: <p>I am working on this for hours and its driving me nuts!</p>
  
  <p>so i think its a really tough one hence the detailed question.</p>
  
  <p>The problem is: </p>
  
  <ul>
  <li><p>I have a server that accepts file post uploads by php script.</p></li>
  <li><p>The data will be distributed to several discs.</p></li>
  <li><p>I want to save the file to the right disc during upload (tmp file).</p></li>
  </ul>
  
  <p>Because: </p>
  
  <ul>
  <li><p>The file needs to be moved later, and i don't want it to take long (copy from the systems tmp directory to target disk)</p></li>
  <li><p>The system drive is an ssd that won't be very happy when so much data is written and deleted on it.</p></li>
  </ul>
  
  <p>This server config changes the init value. during an upload an can see the temp file and see how its filesize increases:</p>
  
  <pre><code>Alias /thundercloud /home/thundercloud
  &lt;Directory /home/thundercloud&gt;
      php_admin_value upload_tmp_dir /home/thundercloud/locations/42/tmp
  &lt;/Directory&gt;
  </code></pre>
  
  <p>My sripts are in <em>/home/thundercloud</em> and there are several symlinks in <em>/home/thundercloud/locations</em> (named numerically 1-n) that all point to the according mountpoints.</p>
  
  <p>Now what i need is to get the number 42 (which is disk no. 42  - no there are only 14 disks in the server, dis his just for test) to be dynamic.</p>
  
  <p>I have a lot of control over the request so I basically don't care whether its determined by hostname, get variable, subdirectory etc.</p>
  
  <p>So my first attempt was this:</p>
  
  <pre><code>RewriteLock /var/lock/apache2/rewrite.lock
  Alias /thundercloud /home/thundercloud
  RewriteMap getuplaodlocation prg:/home/thundercloud/uploadloc.php
  RewriteEngine On
  &lt;Directory /home/thundercloud&gt;
      AllowOverride All
      php_admin_value upload_tmp_dir ${getuplaodlocation:%{THE_REQUEST}}
  &lt;/Directory&gt;
  </code></pre>
  
  <p>the corresponding rewrite map was:</p>
  
  <pre><code>#!/usr/bin/php
  &lt;?
  set_time_limit(0); # forever program!
  $keyboard = fopen("php://stdin","r");
  while (1) {
      $line = trim(fgets($keyboard)); // dont care for now
      echo '/home/thundercloud/locations/42/tmp'.PHP_EOL;
  }
  </code></pre>
  
  <p>I jut hardcoded 42. if it worked i would have parsed the correct falue from <code>THE_REQUEST</code> . It did not work. the script was chmod 777 and everything.</p>
  
  <p>My next attempt was:</p>
  
  <pre><code>Alias /thundercloud /home/thundercloud
  &lt;Directory /home/thundercloud&gt;
      SetEnvIfNoCase Host lionel\.2x\.to upload=/home/thundercloud/locations/42/tmp
      php_admin_value upload_tmp_dir upload
  &lt;/Directory&gt;
  </code></pre>
  
  <p>no luck.</p>
  
  <p>so my final attempt was:</p>
  
  <pre><code>Alias /thundercloud /home/thundercloud
  &lt;Directory /home/thundercloud&gt;
      php_admin_value upload_tmp_dir /home/thundercloud/locations/%{REMOTE_HOST}/tmp
  &lt;/Directory&gt;
  </code></pre>
  
  <p>no luck either. i printed ini_get_all() and got the following output:</p>
  
  <pre><code>["upload_tmp_dir"]=&gt;
    array(3) {
      ["global_value"]=&gt;
      string(47) "/home/thundercloud/locations/%{REMOTE_HOST}/tmp"
  </code></pre>
  
  <p>so obviously this "constant" isn't available like this in this scope </p>
  
  <p>now i ran out of ideas</p>
  
  <p>i must admin my apporaches are really trial and error.</p>
  
  <p>anyony can point me in the right direction or tell me its not possible and justify that?</p>
  
  <p>ps: it obviously doesnt work at php runtime ;)</p>
  
  <p>edit:</p>
  
  <p>Ok after some more hours and a useful comment from Marc B I came up with this "solution":</p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
      ServerName 42.thundercloud.lionel.2x.to
      DocumentRoot /home/thundercloud
      php_admin_value upload_tmp_dir /home/thundercloud/locations/42/tmp
  &lt;/VirtualHost&gt;
  &lt;VirtualHost *:80&gt;
      ServerName 43.thundercloud.lionel.2x.to
      DocumentRoot /home/thundercloud
      php_admin_value upload_tmp_dir /home/thundercloud/locations/43/tmp
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>Just brute force an own VirtualHost container for every disk... its ugly and I hoped to avoid that step and have a generic webserver config, but it seems like the only way to go - atleast it works.</p>
  
  <p>I still wonder whether theres a nicer way and i think its very interesting to go so deep into webserver configurations. Apache is incredibly flexible and its getting really interesting when you push it to the limits (thats at least what i think).</p>
  
  <p>Thats why I will leave this question open and put a bounty on it.</p>
  
  <p>edit:</p>
  
  <p>After some more research I think there might be a solution applying the techniques here:
  <a href="http://httpd.apache.org/docs/2.0/vhosts/mass.html" rel="nofollow">http://httpd.apache.org/docs/2.0/vhosts/mass.html</a></p>
  
  <p>something like:</p>
  
  <pre><code>&lt;VirtualHost *&gt; 
  Use CanonicalName off
  VirtualDocumentRoot /home/thundercloud/locations/%0/tmp/
  php_admin_value open_basedir VIRTUAL_DOCUMENT_ROOT
  &lt;VirtualHost&gt;
  </code></pre>
  
  <p>This is just an idea, not tested yet. still trying to figure it out. The paths are not correct in this case but there might be a workaround with symlinks. Would be nice if some1 could point me in the right direction.</p>