【发布时间】:2012-10-25 02:29:35
【问题描述】:
我需要使用我的 php 脚本修改文件日期更改。在我的本地主机上,我使用了在 Windows 7 中运行的 XAMPP。使用 PHP touch 没有问题,并且可以正常工作。
然而,当我将它上传到我的产品时,LINUX OS PHP touch 不再工作了。我对其进行了调查,发现 Linux 不允许 PHP touch 或不允许任何人更改文件修改日期。
这就是我改用exec("touch filename.txt") 并且它工作正常的原因,但是当我使用此代码时
exec("touch -t 201204040000.00 filename.txt");
它没有做它必须做的事情,我在这里遗漏了什么吗?
这些是我的参考资料:
编辑
ls -l filename.txt
-rw-r--r-- 1 2012-11-04 12:00 filename.txt //supposed that 2012-11-04 12:00 is the original mod date of the file
如果我运行这段代码:
exec("touch filename.txt");
ls -l filename.txt
-rw-r--r-- 1 2012-11-05 11:00 filename.txt //supposed that 2012-11-05 11:00 is the current timestamp
正如大家所见,上面的代码在我身上正常工作。 但如果我这样运行它:
exec("touch -t 201204040000.00 filename.txt");
ls -l filename.txt
-rw-r--r-- 1 2012-11-05 11:00 filename.txt //The mod date doesn't changed at all.
【问题讨论】:
-
尝试
touch()文件时遇到什么错误?这可能与您的 exec 无法正常工作的原因相同。您可能从 exec() 收到一条错误消息 -
执行者的回报是什么?
-
我假设您已经检查了运行 PHP 脚本的用户(最有可能也运行 Apache 的用户)是否具有对您尝试
touch的文件的访问和修改权限。这在 Windows 上不是问题,因为用户是同一个人。 -
你也试过运行不同的命令来检查你是否真的有 shell 访问权限?
<?php echo shell_exec('whoami');将是一个好的开始;) -
@Pekka 没有错误,
touch()是布尔值,所以它只会给出真或假。我在 linux 中读到,如果它不是所有者,它不允许 PHP 触摸,奇怪的是,如果我不包含-t 201204040000.00它运行正常。但是,根据我下面的参考,我的语法没问题。