Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Can't find tmp directory

Tags:

php

apache

tmp

I am having problems with functions that create files in the tmp directory such as tmpfile() and tempnam(). They all seem to fail to write to tmp and return false. upload_tmp_dir is set in php ini and file uploads work fine.

When debugging this error I found that sys_get_temp_dir() gets the location of the tmp directory unfortunately it's not supported in my PHP version (5.1.6). I also saw that using the following method replaces the functionality of sys_get_temp_dir():

if ( !function_exists('sys_get_temp_dir')) {
  function sys_get_temp_dir() {
    if (!empty($_ENV['TMP'])) { return realpath($_ENV['TMP']); }
    if (!empty($_ENV['TMPDIR'])) { return realpath( $_ENV['TMPDIR']); }
    if (!empty($_ENV['TEMP'])) { return realpath( $_ENV['TEMP']); }
    $tempfile=tempnam(__FILE__,'');
    if (file_exists($tempfile)) {
      unlink($tempfile);
      return realpath(dirname($tempfile));
    }
    return null;
  }
}

But there is no reference to a tmp directory in the $_ENV array and tempnam() fails as I mentioned before.

Also open_basedir is not set which I've heard can cause similar problems

How can I find out where the tmp directory is or whether it is even set?
Is this a apache server configuration issue or a PHP one?

Thanks for your help

like image 694
DonutReply Avatar asked Jun 21 '11 12:06

DonutReply


People also ask

Where can I find temporary files in PHP?

PHP : Exercise-28 with Solution Write a PHP script to get the directory path used for temporary files. PHP Code: <? php // Create a temporary file in the temporary // files directory using sys_get_temp_dir() $temp_file = tempnam(sys_get_temp_dir(), 'Tux'); echo $temp_file.

What is sys_ get_ temp_ dir?

sys_get_temp_dir(): string. Returns the path of the directory PHP stores temporary files in by default.

Is tmp in memory or disk?

tmpfs (short for Temporary File System) is a temporary file storage paradigm implemented in many Unix-like operating systems. It is intended to appear as a mounted file system, but data is stored in volatile memory instead of a persistent storage device.

What is Upload_tmp_dir?

Overview. The temporary directory is used for storing files when a file is uploaded. This directory must be writable by whichever user PHP is running as. If not specified, PHP will use the system's default.


1 Answers

I am running Ubuntu 18.04 and I could create/modify files in the /tmp directory when I ran the PHP script from the CLI, but when I tried accessing the same script as a web page, I could never find the file that was being created. It turns out that Apache by default will create a private tmp directory. The following post provided some insight on the problem Odd Bits - Private /tmp directory. However, the /usr/lib/systemd directory mentioned in the post did not contain any services for http or apache2 on my machine. To help track down the problem I executed the following command:

sudo find / -mount -type f -exec grep -e "PrivateTmp" '{}' ';' -print

and found in /lib/systemd/system/apache2.service the PrivateTmp=true mentioned in the Odd Bits post. Copying the file from /lib/systemd/system to /etc/systemd/system/ and changing true to false and executing

systemctl daemon-restart
systemctl restart apache2

fixed the problem. A person wiser than me suggested copying the file to /etc instead of editing it in /lib was the correct course of action because /lib is 'owned' by the packages and local edits should be performed in /etc. systemd man page describes the systemd configuration processing in gory details.

like image 116
One In a Million Apps Avatar answered Oct 03 '22 08:10

One In a Million Apps