PHP找不到tmp目录

发布于 2024-11-16 05:48:04 字数 905 浏览 6 评论 0原文

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

调试此错误时,我发现 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;
  }
}

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

另外 open_basedir 未设置,我听说这可能会导致类似的问题

我如何找出 tmp 目录在哪里或者是否已设置?
这是 apache 服务器配置问题还是 PHP 问题?

感谢您的帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

疯了 2024-11-23 05:48:04

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

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

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

systemctl daemon-restart
systemctl restart apache2

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

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.

两相知 2024-11-23 05:48:04

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

upload_tmp_dir=/your-www/tmp/

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

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

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/');
水水月牙 2024-11-23 05:48:04

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

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

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.

动听の歌 2024-11-23 05:48:04

可能不是最干净的,但这适用于我旧的 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;
}

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;
}
勿忘初心 2024-11-23 05:48:04

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

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

不再见 2024-11-23 05:48:04

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

我的问题是,我正在将上传表单从 /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")

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文