3

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
  • "however nothing worked." - Did you try other methods? .htaccess will only work if PHP is enabled as an Apache module. Are .htaccess overrides enabled in the server config? Have you tried .user.ini? Have you tried just setting the value in your PHP script?
    – MrWhite
    Commented Dec 28, 2020 at 0:12
  • PHP doesn't silently fail (unless you tell it to) - what do your logs say? Who owns the tmp/ dir? stackoverflow.com/questions/2900690/… Commented Jan 2, 2021 at 10:47
  • Did you tried my solution? Is it working for you or you are keep facing issues with this?
    – GTsvetanov
    Commented Jan 3, 2021 at 17:33

3 Answers 3

4
+50

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>
3
  • Thank you so much for this and I feel like I am on the right track, but nothing is still happening. I have updated my answer to show more details. Could you review it please?
    – devwk
    Commented Jan 6, 2021 at 2:58
  • @devwk you want to store session files there too or what?
    – GTsvetanov
    Commented Jan 6, 2021 at 9:01
  • I see what I need to do now. I have solved this problem and will award the bounty to you :) Thank you
    – devwk
    Commented Jan 7, 2021 at 5:54
1

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.

0
0

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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.