📜 ⬆️ ⬇️

zip extension, small hint

One of my projects is widely used to import various data in CSV format. Once there was little data, and everyone was comfortable with directly downloading one or two not too large files. Over time, the data became more, the size of the imported files and their number increased. It was decided to upload files in a ZIP-archive: we save traffic + we have only one upload instead of a few.

To work with the archive, I used the zip extension. I will not describe here what's what in this extension, it is described in detail in the documentation.

I will tell only about one feature that turned out to be useful for me. When you import a file that a user uploads in a form, the ZIP archive on the server itself is useless, and you also need to unpack it unnecessarily: after that, you will also have to delete it. The ZipArchive-> getStream () method comes to the rescue . We get with it the file handler and we can safely feed it, for example, in fgetcsv .
')
UPD. Probably not quite clear explained. I need data from those CSV files that are in the ZIP. And I need this data in the database, and not in the TsVShkah, and certainly not in the archive. That is, I import data from CSVshek into the database. Therefore, I take a warm temporary file ($ _FILES ['userfile'] ['tmp_name']). And without directly unpacking it, I import all the TsSVshki contained in it into the database. After this, PHP itself will take care of deleting the archive, which is no longer needed.

UPD. From the Adelf habraiser : in some cases (not mine) it should be taken into account that $ _FILES ['userfile'] ['tmp_name'] may be unavailable due to open_basedir

I hope this information will be useful to someone.

Source: https://habr.com/ru/post/51907/


All Articles