Home >Backend Development >PHP Tutorial >PHP file packaging Download and use PHP's own ZipArchive to compress the file and download the packaged file_PHP tutorial
Summary:
The operation of downloading a file using PHP requires four header(). You can refer to my other blog post: How PHP implements the download function. Super detailed process analysis
When calculating the size of the file , you don’t need to open the file first. You can see from filesize($filename) that if you need to open the file first, filesize may be in the form filesize($filehandle)
What sends data back to the client is , remember to set a buffer to specify how much data to output to the client each time, such as: $buffer=1023. If not specified, the entire file will be written into the memory, and the data will be transmitted to the client all at once
Through the feof() function, you can determine whether the file to be read has been read. If it has not been read yet, After finishing, continue to read the file ($file_data=fread()), and send the data back to the client (echo $file_data)
After each download is completed, the client will refresh, which shows that, in fact, every time Write the data into a temporary file, and after all downloads are completed, reintegrate all the data together
Here I use absolute paths. The advantage of absolute paths is that they are more adaptable and relatively Compared with relative paths, it is more efficient (eliminating the process of finding files)
Analyze the technical points:
Package files into zip format
Function of downloading files
Key points analysis:
Here I use the ZipArchive class that comes with php
a) We only need to create a new ZipArchive object, and then use the open method to create a zip file. Then use the addFile method to write the file to be packaged into the zip file just created. It is best to remember to close the object.
b) Note: When using the open method, the second parameter $flags is optional. $flags is used to specify the processing method of the opened zip file. There are four situations
i. ZIPARCHIVE::OVERWRITE always creates a new file. If the specified zip file exists, it will be overwritten
ii. ZIPARCHIVE::CREATE If the specified zip file does not exist, a new one will be created.
iii. ZIPARCHIVE::EXCL If the specified zip file exists, an error will be reported
iv. ZIPARCHIVE::CHECKCONS
File downloading process:
Server-side work:
The client's browser sends a request to handle the downloaded php file.
Note: Any operation needs to be written to the memory first. Whether it is a video, audio or text file, it needs to be written to the memory first.
In other words, the operation of reading the files on the "server" into the memory of the "server" is essential (note: here I put the word server in double quotes, mainly to illustrate this A series of operations are completed on the server).
Since you want to write the file into the memory, you must open the file first
So here are three file operation functions:
One: fopen($filename,$ mode)
Two: fread ( int $handle , int $length )
Three: fclose ( resource $handle )
Client side work:
So, how to pass the file information flow that already exists in the server-side memory to the client?
The answer is through the header() function, the client knows how to process the file, whether to save or open, etc.
The final effect is as shown below:
name | Size (KB) | |