PHP tmp upload directory for multlple domains

Viewed 764

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?

3 Answers

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>

UPDATE:

Try adding this in your vhost configration file:

php_admin_value open_basedir none

It temporarily turns off open_basedir, if that works, means you need to change your open_basedir path.


From PHP manual

upload_tmp_dir string

The temporary directory used for storing files when doing file upload. Must be writable by whatever user PHP is running as. If not specified PHP will use the system's default.

If the directory specified here is not writable, PHP falls back to the system default temporary directory. If open_basedir is on, then the system default directory must be allowed for an upload to succeed.

So make sure the directory is writable and has sufficient permission, create it of not exists.

That can be done by adding the following line in your php.ini file:

upload_tmp_dir = /tmp or upload_tmp_dir = /tmp/whatever

Example :

; http://php.net/upload-tmp-dir
upload_tmp_dir="D:\xampp\tmp"

Alternatively, you can set for just one domain, by adding the following line in the domain's vhost.conf file.

<Directory /path/to/vhosts/example.com/httpdocs>
   php_admin_value upload_tmp_dir /tmp
</Directory>

Ensure that this location is writable by the appropriate user/group permission 777 check your phpinfo(); to see what it set to. after you made changes.

Related