PHP安全下载多个文件并保存它们的方法
我需要一种方法来使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您能够遵循相当简单的文件格式规范 并愿意依靠安装的 zlib 扩展来处理解压。
否则,发明您自己的自定义存档文件格式,这并不难。它可能非常简单,如下所示:
所以一个非常简单的存档文件可能看起来像这样
您当然可以通过以二进制格式存储长度(例如使用带有“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:
So a very simple archive file might look like
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.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.