Search code examples
phpuploading

How to dynamically change upload_tmp_dir via an apache module version of PHP dynamically, i.e. before the uploading file is written to disk


I want to rotate the location where temporary files are written however it is my understanding that the PHP script wont execute until after the full post is already completed. By that time the file is already written to the default temp location from the configuration file which is loaded in at the time apache is spawned and the php module is loaded. Any ideas?


Solution

  • As you mention, it isn't possible to change it dynamically since the upload is completed before the page executes -- i.e. no fancy ini_set calls can be made.

    A possible workaround:

    You could set the temporary upload location in your php.ini file to point to a symlink (/tmp/myuploads) that in turn points to one of your chosen upload locations (/mnt/uploadstore_1/).

    You can then have the PHP file that handles the upload change the symlink location to a new location each time it runs ( basically the equivalent of a 'rm /tmp/myuploads && ln -s /mnt/uploadstore_2/ /tmp/myuploads').

    This should make sure that the upload that comes next ends up in your next-in-line chosen location.