PHP安全下载多个文件并保存它们的方法

发布于 2024-10-25 17:43:32 字数 180 浏览 2 评论 0原文

我需要一种方法来使用 PHP 下载大量文件并将它们保存到文件系统中。这是一个部署工具,我计划使用 Zip 文件,因为我只调用一些解压缩代码,而且很简单。然而,ZIP 功能并不是每个人都具备的扩展功能。

我现在唯一的另一个想法是将所有内容压缩为 Base64 编码的一个文件,对其进行解码并将其写回。这并不理想(或者是吗?)。建议?

I need a way to download a lot of files and save them to the file system with PHP. This is for a deployment tool, and I was planning on using a Zip file, since I just call some unzip code and it's simple. However, the ZIP functionality is an extension that not everyone has.

My only other idea right now is to compress everything into one file as Base64 encoded, decode it and write it back out. This isn't ideal (Or is it?). Suggestions?

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

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

发布评论

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

评论(2

半仙 2024-11-01 17:43:32

如果您能够遵循相当简单的文件格式规范 并愿意依靠安装的 zlib 扩展来处理解压。

否则,发明您自己的自定义存档文件格式,这并不难。它可能非常简单,如下所示:

  • 前 4 个字节:存档中的文件数
  • 接下来 4 个字节:文件名中的字节数
  • 接下来 N 个字节:文件名
  • 接下来 10 个字节:文件中的字节数
  • 接下来 N 个字节:文件content
  • 根据需要重复上述 4 行

所以一个非常简单的存档文件可能看起来像这样

00020007foo.txt0000000012Hello world!0007bar.txt0000000010It worked!

您当然可以通过以二进制格式存储长度(例如使用带有“N”格式的 pack)、添加校验和来改进它, 等等。但这样解释起来更容易。

Zip is really not difficult to parse, if you can follow a rather simple file format specification and are willing to count on the zlib extension to be installed to handle the decompression.

Otherwise, invent your own custom archive file format, it's not hard. It could be something a dead simple as this:

  • First 4 bytes: Number of files in the archive
  • Next 4 bytes: Number of bytes in the filename
  • Next N bytes: Filename
  • Next 10 bytes: Number of bytes in the file
  • Next N bytes: File contents
  • Repeat the above 4 lines as necessary

So a very simple archive file might look like

00020007foo.txt0000000012Hello world!0007bar.txt0000000010It worked!

You could of course improve it by storing lengths in binary format (e.g. using pack with the 'N' format), adding checksums, and so on. But it was easier to explain this way.

谈下烟灰 2024-11-01 17:43:32

Base64 只是将一大块数据转换为可以通过电子邮件传输的“安全”格式,然后可以将其解码回其本机 8 位格式。它不允许将多个文件存档/压缩到单个文件中。您可以将多个单独的 Base64 编码文件嵌入到 MIME 消息之类的文件中,这确实允许多个不同的附件。但随后您又回到了同样的问题 - 并非所有内容都会本机处理 MIME 消息,因为那是电子邮件的领域。

Zip是您最好的选择。它有适用于世界上每个主要操作系统的版本,并且内置于两大操作系统(MacOS 和 Windows)的文件管理器中。其他替代方案(rar、7z 等)则不太常见,并且绝对没有机会获得开箱即用的支持。

base64 just translates a chunk of data into a format that's "safe" to transmit via email, which can then be decode back into its native 8bit format. It doesn't allow multiple files to be archived/compressed into a single file. You could embed multiple seperate base64-encoded files in something like a MIME message, which does allow multiple different attachments. But then you're back to the same problem - not everything will natively handle MIME messages, since that's the domain of email.

Zip is your best choice. There's versions of it available for every major OS under the sun, and is built into the file managers for the big 2 - MacOS and Windows. The other alternatives (rar, 7z, etc..) are far less common and definitely have no chance of out-of-the-box support.

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