5

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

7
  • 1
    Check your php.ini. You should find the directory in there.
    – Colum
    Jun 21, 2011 at 12:15
  • 1
    what would it be called? thanks
    – DonutReply
    Jun 21, 2011 at 12:17
  • There are different variables. I found the directory under upload_tmp_dir and session.save_path. But the PHP default is /tmp
    – Colum
    Jun 21, 2011 at 12:39
  • 2
    TMP, TEMP (and maybe TMPDIR) are valid on Windows only and usually pointing to C:\Windows\TEMP. On Linux default temp location is /tmp. To workaround this (works with tempnam() function) you can create a temp folder somewhere within your website space, specify appropriate access permissions and pass this as first parameter to the above function. No a great solution, but better than nothing.
    – LazyOne
    Jun 21, 2011 at 13:41
  • 1
    @Ollie If you insist, then why not :)
    – LazyOne
    Jun 22, 2011 at 14:00

6 Answers 6

4

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.

1
  • Systemd is such an annoying mess.
    – ceving
    Apr 16, 2019 at 12:07
2

you can set the upload temp dir in your php.ini - something like that should work:

upload_tmp_dir=/your-www/tmp/

Also, in case you can't edit the php.ini or don't want to do it globally you can use this in the beginning of your script:

ini_set('upload_tmp_dir','/your-home-www/tmp/');
1

TMP, TEMP (and maybe TMPDIR) are valid on Windows only and usually pointing to C:\Windows\TEMP. On Linux default temp location is /tmp. To workaround this (works with tempnam() function) you can create a temp folder somewhere within your website space, specify appropriate access permissions and pass this as first parameter to the above function.

Not a great solution but better than nothing.

0

Probably not the cleanest but this works on my old 5.1.6 install:

function get_temp_path() {
  static $path = null;
  if ($path !== null) return $path;
  $file = tmpfile();
  $meta = stream_get_meta_data($file);
  fclose($file);
  $path = dirname($meta['uri']);
  return $path;
}
0

I have the same problem and the solution is to change the apache configuration to expose the TEMP variable to PHP, see this post.

0

Tip for newbies like me: I THOUGHT that PHP couldn't move stuff from my temporary folder, but I was just confused because of the relative positions of folders. This may apply to someone else, so I'll explain, even though it's very tangentially related to this specific question (because this specific question is a likely search result for other people like me who are newbies).

My problem is that I was echoing an upload form FROM a functions.php file inside of /var/www/html/ TO a profile.php file in /var/www/html/user/ which CALLED an uploadphoto.php file in /var/www/html/. Uploaded files were ultimately intended to land in /var/www/html/uploads. This ultimately meant that most of my references to both uploadphoto.php AND uploads/ in functions.php were written "../uploadphoto.php" or "../uploads/[etc.jpg]", respectively, in order to step back into html/ from html/user/ (where the echoed code landed in html/user/profile.php). This led me to intuitively use the following command inside of uploadphoto.php without thinking it through:

move_uploaded_file($_FILES["file"]["tmp_name"][0], "../uploads/$filename")

See the problem? uploadphoto.php is in the same directory as uploads/, so I did not need the ../ here. For hours, I was sure I was messing up my folder permissions again, because I am new to image uploading. I had forgotten to check for more simple-minded errors. Hopefully this helps someone else.

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.