简体   繁体   English

mkdir在/ tmp目录中失败

[英]mkdir fails in the /tmp directory

I'm trying to create a folder in php and the code kind of fails each it is used with /tmp/... as path: 我正在尝试在php中创建一个文件夹,并且每个与/tmp/...一起使用的代码都失败了作为路径:

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. 但是,如果我检查/tmp -Folder没有这样的目录,并且该文件夹上的所有后续写入或读取操作都会失败,因为该文件夹不存在。 The permissions for /tmp are correct (root:root with 777) and i can do sudo -u http mkdir -p /tmp/test without problems. /tmp的权限是正确的(root:root with 777),我可以毫无问题地执行sudo -u http mkdir -p /tmp/test 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 ... ) 如果我使用tmp/test例如,代码将运行正常并在php-skript的目录中创建一个文件夹(它位于属于我的文件夹中,而不是http用户......)

Any ideas as to why php fails to create a folder under /tmp/ but reports it as being there? 关于为什么php无法在/tmp/下创建文件夹但是将其报告为存在的任何想法?

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. 编辑:指定读取和写入操作:这些操作不是来自我自己的脚本,而是来自外部skripts,由php脚本调用以执行不同的任务。 Once all of them succeeded, the folder gets zipped and copied somewhere else. 一旦所有这些文件成功完成,文件夹就会被压缩并复制到其他地方。

Edit: Right after running exec("mkdir -p /tmp/testfolder"); 编辑:运行exec("mkdir -p /tmp/testfolder");后右键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. 编辑:事实证明,这不是php的问题,而是systemd / apache。 In short: systemd creates a private tmp-folder for apache while running, which resides under /tmp/systemd-private-XYZ. 简而言之:systemd在运行时为apache创建一个私有tmp文件夹,它位于/ tmp / systemd-private-XYZ下。 So the real /tmp is not viewable by the php-skript, but rather the private one. 所以真正的/tmp不是由php-skript可见的,而是私有的。 See http://blog.oddbit.com/post/private-tmp-directories-in-fedora for more infos. 有关更多信息,请参阅http://blog.oddbit.com/post/private-tmp-directories-in-fedora

As it turns out, this is not a problem with php, but rather with systemd / apache. 事实证明,这不是php的问题,而是systemd / apache。 In short: systemd creates a private tmp-folder for apache while running, which resides under /tmp/systemd-private-XYZ. 简而言之:systemd在运行时为apache创建一个私有tmp文件夹,它位于/ tmp / systemd-private-XYZ下。 So the real /tmp is not viewable by the php-skript, but rather the private one. 所以真正的/ tmp不是由php-skript可见的,而是私有的。

To disable this behavior, you can set PrivateTmp=false in /usr/lib/systemd/system/httpd.service 要禁用此行为,您可以在/usr/lib/systemd/system/httpd.service中设置PrivateTmp = false

See http://blog.oddbit.com/2012/11/05/fedora-private-tmp/ for more infos. 有关更多信息,请参见http://blog.oddbit.com/2012/11/05/fedora-private-tmp/

Don't do that. 不要那样做。 Use PHP's awesomely called function, tmpfile() . 使用PHP的非常棒的函数tmpfile() From the docs: 来自文档:

$temp = tmpfile();
fwrite($temp, "writing to tempfile");
fseek($temp, 0);
echo fread($temp, 1024);
fclose($temp); // this removes the file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM