PHP Safe way to download mutliple files and save t

2019-09-02 06:49发布

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?

标签: php zip
2条回答
祖国的老花朵
2楼-- · 2019-09-02 07:25

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.

查看更多
Lonely孤独者°
3楼-- · 2019-09-02 07:40

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.

查看更多
登录 后发表回答