2

I'm trying to create a folder in php and the code kind of fails each it is used with /tmp/... as path:

exec("mkdir -p /tmp/test/ 2>&1", $output, $return_code);
// $output is empty, $return_code is 0
//mkdir("/tmp/test/"); // Alternative to above

is_dir("/tmp/test/"); // returns true
is_readable("/tmp/test/"); // returns true

But if i check the /tmp-Folder there is no such directory and all subsequent write or read operations on the folder fail, because the folder does not exist. The permissions for /tmp are correct (root:root with 777) and i can do sudo -u http mkdir -p /tmp/test without problems. If I use tmp/test for example, the code will run fine and create a folder within the directory of the php-skript (Which lies in a folder which belongs to me, not the http-user ... )

Any ideas as to why php fails to create a folder under /tmp/ but reports it as being there?

Edit: To specify read- and write-actions: Those actions are not from within my own script, but rather external skripts which get called by the php-script to execute different tasks. Once all of them succeeded, the folder gets zipped and copied somewhere else.

Edit: Right after running exec("mkdir -p /tmp/testfolder");

[daishy@littlezombie tmp]$ pwd
/tmp
[daishy@littlezombie tmp]$ ls -al
insgesamt 8
drwxrwxrwt 21 root   root   440  3. Aug 18:56 .
drwxr-xr-x 20 root   root  4096 10. Jun 16:49 ..
drwxrwxrwt  2 root   root    40  3. Aug 09:42 .font-unix
drwxr-xr-x  2 daishy users   60  3. Aug 14:40 hsperfdata_daishy
drwxrwxrwt  2 root   root    60  3. Aug 09:42 .ICE-unix
drwx------  2 daishy users   60  3. Aug 12:35 kde-daishy
drwx------  2 daishy users  140  3. Aug 18:49 ksocket-daishy
drwx------  3 root   root    60  3. Aug 18:54 systemd-private-5rIfGj
drwx------  3 root   root    60  3. Aug 09:42 systemd-private-HGNW9x
drwx------  3 root   root    60  3. Aug 09:42 systemd-private-od4pyY
drwx------  3 root   root    60  3. Aug 09:42 systemd-private-qAH8UK
drwxrwxrwt  2 root   root    40  3. Aug 09:42 .Test-unix
drwx------  4 daishy users   80  3. Aug 16:55 .Trash-1000
-r--r--r--  1 root   root    11  3. Aug 09:42 .X0-lock
drwxrwxrwt  2 root   root    60  3. Aug 09:42 .X11-unix
drwxrwxrwt  2 root   root    40  3. Aug 09:42 .XIM-unix

Edit: As it turns out, this is not a problem with php, but rather with systemd / apache. In short: systemd creates a private tmp-folder for apache while running, which resides under /tmp/systemd-private-XYZ. So the real /tmp is not viewable by the php-skript, but rather the private one. See http://blog.oddbit.com/post/private-tmp-directories-in-fedora for more infos.

Daishy
  • 238
  • 2
  • 11
  • 4
    php has functions to make tmp files, why not use them? – MightyPork Aug 03 '13 at 16:15
  • To add to MightyPork: http://us1.php.net/manual/en/function.tmpfile.php The function is awesomely called... `tmpfile()` – Jim Aug 03 '13 at 16:20
  • I'm not trying to a create single a temporary file, i need a new folder for various reasons (Some exec-calls to other skripts in there as well, that store data in there as well). I could create a temporary folder somewhere else, but i still would be interested in why php fails to create a folder under /tmp. Edit: Put some additional infos specifing read/write-actions in the question. – Daishy Aug 03 '13 at 16:39
  • Can you create a folder using bash instead of PHP? – Cole Tobin Aug 03 '13 at 17:00
  • Yes, as i mentioned in my post i can do `sudo -u http mkdir -p /tmp/test` – Daishy Aug 03 '13 at 17:05

2 Answers2

8

As it turns out, this is not a problem with php, but rather with systemd / apache. In short: systemd creates a private tmp-folder for apache while running, which resides under /tmp/systemd-private-XYZ. So the real /tmp is not viewable by the php-skript, but rather the private one.

To disable this behavior, you can set PrivateTmp=false in /usr/lib/systemd/system/httpd.service

See http://blog.oddbit.com/2012/11/05/fedora-private-tmp/ for more infos.

Stickley
  • 4,561
  • 3
  • 30
  • 29
Daishy
  • 238
  • 2
  • 11
  • 1
    For anyone finding this now, this seems to be the updated link for Oddbits: http://blog.oddbit.com/2012/11/05/fedora-private-tmp/ – Nicholas Smith Jan 22 '16 at 15:08
2

Don't do that. Use PHP's awesomely called function, tmpfile(). From the docs:

$temp = tmpfile();
fwrite($temp, "writing to tempfile");
fseek($temp, 0);
echo fread($temp, 1024);
fclose($temp); // this removes the file
Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
  • See answer to comment on the question. – Daishy Aug 03 '13 at 16:39
  • 1
    Maybe your not supposed to create folders under /tmp? – Cole Tobin Aug 03 '13 at 16:42
  • Yeah, thats basicly my question, as i did find nothing as to why i wouldn't be allowed to write in the folder that exists to hold temporary data. And, even if thats the case, why is_dir and is_readable return true, if the folder was not created. – Daishy Aug 03 '13 at 16:44
  • Can you verify they aren't created. Like a screenshot of the folder after calling mkdir? – Cole Tobin Aug 03 '13 at 16:46
  • Sooo, while adding the list-output above i found my folders where actually created, but in the `/tmp/systemd-private-X` folders. It seems apache/php reroutes `/tmp` internaly to some temporary-temporary folder. `is_dir` seems to use the modified path, whereas my `tar`-call does not, which is a problem. – Daishy Aug 03 '13 at 17:01
  • Well there's your problem. – Cole Tobin Aug 03 '13 at 17:01
  • What exactly do you mean? The systemd-private-folders or the folder not beeing there? Anyways, as it seems the problem is not mkdir but php or apache (most likely apache) messing with the path (See my previous comment) – Daishy Aug 03 '13 at 17:07