PHP 的 PGP 加密
如果我从 unix 命令提示符运行该命令
gpg -e -r [email protected] my_secret_file.txt
,它工作正常。但是当我尝试使用 PHP 使用相同的命令时,它不起作用:
$gpg = '/usr/bin/gpg';
$recipient = '[email protected]';
$secret_file = 'secret_file.txt';
echo shell_exec("$gpg -e -r $recipient $secret_file");
请指导我找到解决方案。
If I run the command
gpg -e -r [email protected] my_secret_file.txt
from unix command prompt, it is working fine. But when I try to use the same command using PHP, it's not working :
$gpg = '/usr/bin/gpg';
$recipient = '[email protected]';
$secret_file = 'secret_file.txt';
echo shell_exec("$gpg -e -r $recipient $secret_file");
Please guide me for a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了 Demento 所建议的权限问题之外,脚本运行的用户也可能与您在命令行中工作时的用户不同,并且该用户没有 [电子邮件受保护],因此无法为他加密文件。那么...哪个用户运行这个脚本?假设它是
www-data
,如果您看到了 [电子邮件受保护]?
Apart from permission issues as Demento suggests, it is also possible that the user the script runs as is not the same as when you work in the command line, and this user does not have the public key of [email protected], so it cannot encrypt the file for him. So... which user runs this script? Assuming it's
www-data
, if you dodo you see the key for [email protected]?
我在我的机器上尝试了你的示例,它的工作原理与预期一致。结果是本地目录中名为
secret_file.txt.gpg
的附加文件,为[电子邮件受保护]
。这证实您的实际脚本没有问题,并且可能存在其他问题。您不会看到脚本本身的任何输出,加密文件是默默生成的。程序最后一行中的
echo
不执行任何操作,因为如果没有问题,gpg 不会生成任何输出。你应该看看 shell_exec 的手册。如果该文件不是在您的计算机上创建的,则可能是权限问题。如果您不从命令行而是从 Web 应用程序调用脚本,则 Web 服务器用户需要文件系统上的适当权限才能创建新文件。
I tried your example on my machine and it works like expected. The result is an additional file in the local directory named
secret_file.txt.gpg
, encrypted for[email protected]
. This confirms that your actual script is fine and there might be other issues present.You will not see any output from the script itself, the encrypted file is generated in silence. The
echo
in the last line of your program does nothing, because gpg does not generate any output if there are no problems. You should take a look at the manual of shell_exec.If the file is not created on your machine, it might be a permission issue. If you do not call the script from the command line but from a web application, the webserver user needs the appropriate permissions on the file system to create a new file.
您可以使用以下命令查看错误:
这将指导您。
要查看的可能问题:
让我知道您的代码输出,我会能够提供帮助。我已经遇到过这个,这很乏味。
You can see the error by using the following command:
this will guide you.
possible problem to look at:
Let me know what your code outputs and I'll be able to help. I ran into this already, it is tedious.