CGI.pm 通常在哪里创建临时文件?
在我的所有 Windows 服务器上(一台计算机除外),当我执行以下代码来分配临时文件文件夹时:
use CGI;
my $tmpfile = new CGITempFile(1);
print "tmpfile='", $tmpfile->as_string(), "'\n";
变量 $tmpfile
被分配值 '.\CGItemp1'
这就是我想要的。 但在我的一台服务器上,它被错误地设置为 C:\temp\CGItemp1
。
所有服务器都运行 Windows 2003 标准版、IIS6 和 ActivePerl 5.8.8.822(不能升级到更高版本的 Perl)。 从命令行运行脚本或在 IIS 中将脚本作为 CGI 脚本运行时,结果始终相同(其中 scriptmap .pl
= c:\perl\bin\perl.exe "%s “%s
)。
如何修复此 Perl 安装并强制其默认返回“.\CGItemp1
”?
我什至将整个 Perl 文件夹从一台工作服务器复制到这台机器上,但没有什么乐趣。
我检查了“TMP
”和“TEMP
”环境变量以及 $ENV{TMP}
和 $ENV{TEMP}
并且它们是相同的。
它们从命令行指向用户配置文件目录,例如:
C:\DOCUME~1\[用户名]\LOCALS~1\Temp\1
当在 IIS 下作为 CGI 脚本运行时,它们都指向:
c:\windows\temp
在注册表项 HKEY_USERS/.DEFAULT/Environment
中,两台服务器都有:
%USERPROFILE%\Local Settings\Temp
CGITempFile()
的 ActiveState 实现显然使用了替代机制来确定应如何生成临时文件夹。
问题
真正的 与 CGI.pm
模块和附件处理一起使用。 每当文件上传到网站时,CGI.pm
都需要将其存储在临时位置。 为此,在 CGI.pm
中调用 CGITempFile()
来分配临时文件夹。 不幸的是我无法使用File::Temp
。 不管怎么说,还是要谢谢你。
束。 我之前确实快速浏览了 CGI.pm
源代码,但你的建议让我回去更仔细地查看它以了解底层算法。 我一切正常,但最奇怪的是服务器上最初没有 c:\temp
文件夹。
为了获得临时修复,我创建了一个 c:\temp
文件夹,并为网站的匿名用户帐户设置了相关权限。 但因为这是一个共享盒子,所以即使临时文件已被删除,我也不能这样保留。 长话短说,我将 c:\temp
文件夹重命名为不同的名称,并且神奇地返回了正确的“.\
”文件夹路径。 我还注意到客户在网站上启用了 FrontPage 扩展,这会删除匿名用户帐户对网站文件夹的写入权限,因此需要重新申请此权限。 我仍然不知道为什么在本问题开始时 CGITempFile()
返回 c:\temp
,即使该文件夹不存在,并且为什么它神奇地再次开始工作。
On all my Windows servers, except for one machine, when I execute the following code to allocate a temporary files folder:
use CGI;
my $tmpfile = new CGITempFile(1);
print "tmpfile='", $tmpfile->as_string(), "'\n";
The variable $tmpfile
is assigned the value '.\CGItemp1'
and this is what I want. But on one of my servers it's incorrectly set to C:\temp\CGItemp1
.
All the servers are running Windows 2003 Standard Edition, IIS6 and ActivePerl 5.8.8.822 (upgrading to later version of Perl not an option). The result is always the same when running a script from the command line or in IIS as a CGI script (where scriptmap .pl
= c:\perl\bin\perl.exe "%s" %s
).
How I can fix this Perl installation and force it to return '.\CGItemp1
' by default?
I've even copied the whole Perl folder from one of the working servers to this machine but no joy.
I checked the 'TMP
' and 'TEMP
' environment variables and also $ENV{TMP}
and $ENV{TEMP}
and they're identical.
From command line they point to the user profile directory, for example:
C:\DOCUME~1\[USERNAME]\LOCALS~1\Temp\1
When run under IIS as a CGI script they both point to:
c:\windows\temp
In registry key HKEY_USERS/.DEFAULT/Environment
, both servers have:
%USERPROFILE%\Local Settings\Temp
The ActiveState implementation of CGITempFile()
is clearly using an alternative mechanism to determine how it should generate the temporary folder.
The real problem is with the CGI.pm
module and attachment handling. Whenever a file is uploaded to the site CGI.pm
needs to store it somewhere temporary. To do this CGITempFile()
is called within CGI.pm
to allocate a temporary folder. So unfortunately I can't use File::Temp
. Thanks anyway.
That helped a bunch. I did have a quick scan through the CGI.pm
source earlier but your suggestion made me go back and look at it more studiously to understand the underlying algorithm. I got things working, but the oddest thing is that there was originally no c:\temp
folder on the server.
To obtain a temporary fix I created a c:\temp
folder and set the relevant permissions for the website's anonymous user account. But because this is a shared box I couldn't leave things that way, even though the temp files were being deleted. To cut a long story short, I renamed the c:\temp
folder to something different and magically the correct '.\
' folder path was being returned. I also noticed that the customer had enabled FrontPage extensions on the site, which removes write access for the anonymous user account on the website folders, so this permission needed re-applying. I'm still at a loss as to why at the start of this issue CGITempFile()
was returning c:\temp
, even though that folder didn't exist, and why it magically started working again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
临时目录的名称保存在
$CGITempFile::TMPDIRECTORY
中,并在 CGI.pm 中的find_tempdir
函数中初始化。CGI.pm 文档中描述了选择临时目录的算法(搜索-private_tempfiles)。
IIUC,如果服务器上存在 C:\Temp 文件夹,CGI.pm 将使用它。 如果
find_tempdir
中检查的目录都不存在,则当前目录“.” 用来。我希望这有帮助。
The name of the temporary directory is held in
$CGITempFile::TMPDIRECTORY
and initialised in thefind_tempdir
function in CGI.pm.The algorithm for choosing the temporary directory is described in the CGI.pm documentation (search for -private_tempfiles).
IIUC, if a C:\Temp folder exists on the server, CGI.pm will use it. If none of the directories checked in
find_tempdir
exist, then the current directory "." is used.I hope this helps.
不是您问题的直接答案,但您是否尝试过使用 File::Temp ?
它专门设计用于任何操作系统。
Not the direct answer to your question, but have you tried using File::Temp?
It is specifically designed to work on any OS.
如果您正在运行此脚本,请检查 %TEMP% 环境变量以查看它是否不同。
如果 IIS 正在执行,请检查注册表中 TMP 和 TEMP 下的值
HKEY_USERS/.DEFAULT/环境
If you're running this script as you, check the %TEMP% environment variable to see if if it differs.
If IIS is executing, check the values in registry for TMP and TEMP under
HKEY_USERS/.DEFAULT/Environment