Search code examples
phpapache.htaccessubuntutmp

PHP tmp upload directory for multlple domains


I am hosting multiple domains on my Apache Web Server on Ubuntu 18.04 but I cannot set the tmp upload directory for PHP.

I tried putting this in my .htaccess for one of the domains, however nothing was stored when I tested.

php_value upload_tmp_dir /var/www/example.com/tmp/

My permissions for the /var/www/example.com/tmp/ folder are set at Chmod 775

Is there a working way to set this in .htaccess or in the domain's .conf file?


Solution

  • I've made something like that for my vhosts on my local machine. Note that everything is defined in my vhost because you can't change upload_tmp_dir and sys_temp_dir in runtime.

    <VirtualHost *:80>
        ServerName example.local
        ServerAlias www.example.local
        
        UseCanonicalName On
        
        <Directory /mnt/storage/Server/example>
            DirectoryIndex index.php
            #Options +Indexes +FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
        
        php_admin_value upload_tmp_dir /mnt/storage/Server/example/temp/
        php_admin_value sys_temp_dir /mnt/storage/Server/example/temp/
        
        DocumentRoot "/mnt/storage/Server/example"
        
        ErrorLog ${APACHE_LOG_DIR}/example.error.log
        CustomLog ${APACHE_LOG_DIR}/example.access.log combined
    </VirtualHost>
    

    How to confirm that everything works:

    Create simple file:

    $dir = sys_get_temp_dir();
    $file = tempnam($dir, rand());
    var_dump(get_current_user());
    var_dump($dir);
    var_dump('is_writable: ' . (int)is_writable($dir));
    var_dump('is_readable: ' . (int)is_readable($dir));
    var_dump($file);
    

    Upload script: create file named upload.php

    <?php
    if (isset($_POST['submit'])) {
        var_dump($_FILES);
    }
    ?>
    <!DOCTYPE html>
    <html>
        <body>
        <form action="upload.php" method="post" enctype="multipart/form-data">
            Select image to upload:
            <input type="file" name="upload" id="upload">
            <input type="submit" value="Upload Image" name="submit">
        </form>
        </body>
    </html>