PHP:反义词和上传的文件

发布于 2024-12-13 03:50:31 字数 251 浏览 3 评论 0原文

我正在创建一些代码,用于将 Word 文档的内容上传到服务器,提取其文本,然后将其插入数据库。

exec("PATH=$PATH:/home1/myserver/bin && antiword " . 
     $_FILES['file']['tmp_name'], $mycontent);

由于某些奇怪的原因, $mycontent 始终是一个空数组。谷歌并没有那么有帮助。有谁知道我做错了什么?

I'm creating some code that will upload the contents of a word document to a server, extract its text, and insert it into a database.

exec("PATH=$PATH:/home1/myserver/bin && antiword " . 
     $_FILES['file']['tmp_name'], $mycontent);

For some bizarre reason, $mycontent is always an empty array. Google wasn't that helpful. Does anyone know what I'm doing wrong?

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

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

发布评论

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

评论(1

七婞 2024-12-20 03:50:31

exec 引用中的 $PATH 试图转换为 PHP $PATH,而不是 BASH $PATH。

您可以转义 $ (\$) 或使用单引号。

一般来说,您应该使用 escapeshellarg()escapeshellcmd() 来让事情变得更安全。本来可以避免这种情况的发生。此外,如果您使用用户输入调用 exec(),这将有助于防止他们转义您的命令并调用自己的恶意 shell 命令。

编辑
实际上,由于某种原因,您的文件名/路径可能存在问题。就从简单开始吧。

这是否有效:

exec('/home1/myserver/bin/antiword ' . 
          escapeshellarg($_FILES['file']['tmp_name']), $mycontent);

如果没有,这是什么:

echo '/home1/myserver/bin/antiword ' . 
          escapeshellarg($_FILES['file']['tmp_name']);

您必须创建一个文件来测试,并将其替换为 $_FILES 中的文件。但这可以直接从命令行运行吗?

The $PATH in your exec quote is trying to be converted to whatever your PHP $PATH is rather than the BASH $PATH.

You can either escape the $ (\$) or use single quotes.

In general, you should be using escapeshellarg() or escapeshellcmd() to make things a bit safer. It would have prevented this situation. Also if you call exec() with user inputs, it will help prevent them from escaping your command and calling their own malicious shell commands.

EDIT
Actually, you might have issues with your filename/path for some reason. Just start simple.

Does this work:

exec('/home1/myserver/bin/antiword ' . 
          escapeshellarg($_FILES['file']['tmp_name']), $mycontent);

If not, what is this:

echo '/home1/myserver/bin/antiword ' . 
          escapeshellarg($_FILES['file']['tmp_name']);

You'll have to create a file to test, and substitute it for the file in $_FILES. But does that work directly from the commandline?

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