php PHP找不到tmp目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6425034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:21:07  来源:igfitidea点击:

PHP Can't find tmp directory

phpapachetmp

提问by DonutReply

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_diris set in php ini and file uploads work fine.

我在使用在 tmp 目录中创建文件的函数时遇到问题,例如tmpfile()tempnam()。他们似乎都无法写入 tmp 并返回 false。upload_tmp_dir在 php ini 中设置,文件上传工作正常。

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():

调试此错误时,我发现sys_get_temp_dir()获取 tmp 目录的位置很遗憾,我的 PHP 版本 (5.1.6) 不支持它。我还看到使用以下方法替换了以下功能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.

但是 $_ENV 数组中没有对 tmp 目录的引用,并且tempnam()正如我之前提到的那样失败。

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

也没有设置 open_basedir ,我听说这会导致类似的问题

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?

我怎样才能找出 tmp 目录在哪里或者它是否被设置?
这是 apache 服务器配置问题还是 PHP 问题?

Thanks for your help

谢谢你的帮助

采纳答案by LazyOne

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.

TMP、TEMP(可能还有 TMPDIR)仅在 Windows 上有效并且通常指向C:\Windows\TEMP. 在 Linux 上默认的临时位置是/tmp. 要解决此问题(使用tempnam()函数),您可以在网站空间的某处创建一个临时文件夹,指定适当的访问权限并将其作为第一个参数传递给上述函数。

Not a great solution but better than nothing.

不是一个很好的解决方案,但总比没有好。

回答by Ido Green

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

你可以在你的 php.ini 中设置上传临时目录 - 类似的东西应该可以工作:

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:

此外,如果您无法编辑 php.ini 或不想全局执行此操作,您可以在脚本的开头使用它:

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

回答by One In a Million Apps

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:

我正在运行 Ubuntu 18.04,当我从 CLI 运行 PHP 脚本时,我可以在 /tmp 目录中创建/修改文件,但是当我尝试访问与网页相同的脚本时,我永远找不到正在创建的文件. 事实证明,Apache 默认会创建一个私有的 tmp 目录。以下帖子提供了有关Odd Bits - Private /tmp directory问题的一些见解。但是,帖子中提到的/usr/lib/systemd目录在我的机器上没有包含任何http或apache2的服务。为了帮助追踪问题,我执行了以下命令:

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

and found in /lib/systemd/system/apache2.servicethe PrivateTmp=truementioned in the Odd Bits post. Copying the file from /lib/systemd/system to /etc/systemd/system/ and changing trueto falseand executing

并在/lib/systemd/system/apache2.service 中找到了 Odd Bits 帖子中提到的PrivateTmp=true。将文件从 /lib/systemd/system 复制到 /etc/systemd/system/ 并将true更改为false并执行

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 pagedescribes the systemd configuration processing in gory details.

解决了这个问题。一个比我更聪明的人建议将文件复制到 /etc 而不是在 /lib 中编辑它是正确的做法,因为 /lib 由包“拥有”,并且应该在 /etc 中执行本地编辑。systemd 手册页详细描述了 systemd 配置处理。

回答by Pete

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

可能不是最干净的,但这适用于我的旧 5.1.6 安装:

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;
}

回答by Jean Louis

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

我有同样的问题,解决方案是更改 apache 配置以将 TEMP 变量公开给 PHP,请参阅此帖子

回答by Lucas Jarrett

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).

给像我这样的新手的提示:我认为 PHP 无法从我的临时文件夹中移动东西,但我只是因为文件夹的相对位置而感到困惑。这可能适用于其他人,所以我会解释,即使它与这个特定问题非常相关(因为这个特定问题可能是像我这样的新手的其他人的搜索结果)。

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:

我的问题是,我正在从 /var/www/html/ 中的functions.php 文件向 /var/www/html/user/ 中的 profile.php 文件回显上传表单,该文件在 / 中调用了一个 uploadphoto.php 文件var/www/html/. 上传的文件最终打算放在 /var/www/html/uploads 中。这最终意味着我在functions.php中对uploadphoto.php和uploads/的大部分引用分别写成“../uploadphoto.php”或“../uploads/[etc.jpg]”,以便逐步从 html/user/ 回到 html/(其中回显的代码位于 html/user/profile.php 中)。这让我不假思索地在 uploadphoto.php 中直观地使用以下命令:

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.

看到问题了吗?uploadphoto.php 与uploads/ 在同一目录中,所以我不需要../ 在这里。几个小时以来,我确信我再次搞砸了我的文件夹权限,因为我是图像上传的新手。我忘记检查更简单的错误。希望这对其他人有帮助。