Just received such a demand, in fact, I was refused. I even have the patience to discuss with PM, throw a csv not good?
PM: The other party needs a CSV package for. tar.gz, they are Linux server, which is hard to ask.
Then I began to toss the trip, inside the small pits countless.
In fact, there are two general ideas here:
1. Use the system (), the EXEC () function to make the generated CSV into a package using the Systems Command tar, compared to the egg pain is the company with Windows, I can only in their own virtual machine Ubuntu test.
2. Use out-of-the-box tool classes or functions to generate a tar.gz directly, and then throw the CSV in.
I'll pick the second road first. First, the others are going to. tar.gz, I'll check it out. PHP's built-in functions, seemingly only gzwrite,gzopen and other file manipulation functions, can only make the file into a safe binary. gz file. I found that if I walked on to the 1th road, I could only use the system command to achieve it.
I'm trying to exec in Ubuntu (' tar-cxf do.tar.gz do.csv '), however only run script can perform the build success, and the browser does not generate it. Didn't get tar.gz, how to talk behind the use of a series of headers to output the download file?
So the first road was completely dead, and I continued to walk on the second road.
Because the network is not good, can not connect the VPN. I can only keep Baidu, but not the use of Google quality resources. I searched the relevant PHP export tar.gz data, a large number of domestic plagiarism, the basic conclusion is: a few people encounter this demand, and encountered the download of the file is broken can not open, no solution.
You read it wrong, yes: no solution. No one in the country solved the problem (at least on the page that can be searched). I first try to the National salvation, first or the Gzwrite series function to create a. gz file, the code is as follows:
<?PHP$gz=Gzopen('/tmp/do.gz ', ' W9 '); Open or create a. gz file, remember write permission issues under LinuxGzwrite($gz, ' 33c,24ec,32q3 ');//write content to a. gz fileGzclose($gz); Close file handles, freeing resourcesHeader("Content-type:application/x-gzip");$lastPackName= '/tmp/do.gz ';//header ("Content-type:application/octet-stream");Header("Accept-ranges:bytes");Header("Accept-length:".)filesize($lastPackName));Header("Content-disposition:attachment; Filename=records.gz ");@ReadFile($lastPackName);
After you download the file, you want to delete the compressed package
@unlink($lastPackName);
For the use of related functions, you can consult the relevant functions on the php.net itself. Then I found to write a demo file, is able to download the normal decompression files, and if written to the company project in the corresponding controller function inside, the download package is broken, this is the first pit I encountered.
Then I wondered if it was an environmental problem. I have downloaded a good package, throw it into my virtual machine (Ubuntu) inside, also decompression out there is a problem. The original file generated on the server. GZ is intact. So I met the same problem as this classmate: http://bbs.csdn.net/topics/390226194
Finally, his question was said to be the header of the problem set, I have carefully examined the header settings no problem.
I know pear has a Archive_tar class can be directly packaged tar file, I analyzed a bit, need to install pear corresponding package, the cost is large, give up the practice.
Yesterday afternoon in the continuous download decompression failure and view the domestic non-solution of the technical data on the consumption. Back home at night, I am not reconciled, even on the VPN, anger with Google, quickly found this article:
Http://stackoverflow.com/questions/7004989/creating-zip-or-tar-gz-archive-without-exec
Crooked Nuts will toss, have encountered similar packaging requirements, And no exec is used. You can use the Phardata class that comes with PHP to create a tar file and then package it into tar.gz with the Compress function. About Phardata classes and related functions can be consulted on php.net.
So I use the following code to package:
<?PHP$csvFile= '/tmp/do.csv ';$fp=fopen($csvFile, ' w+ ');fwrite($fp, ' 232,C233,23DC ');fclose($fp);$tarFile= '/tmp/archive.tar ';$a=NewPhardata ($tarFile);//ADD FILES to Archive.tar FILE$a->addfile ($csvFile, ' Do.csv ');$a->compress (Phar::GZ);unlink($tarFile);unlink($csvFile);Header("Content-type:application/x-gzip");$lastPackName=$tarFile. '. Gz ';//header ("Content-type:application/octet-stream");Header("Accept-ranges:bytes");Header("Accept-length:".)filesize($lastPackName));Header("Content-disposition:attachment; Filename=records.tar.gz ");@ReadFile($lastPackName);//after you download the file, you want to delete the compressed package@unlink($lastPackName);
I really so easily created a tar.gz file containing xx.csv, but the download decompression failed to persist. Of course, if you write a small script directly with the browser access is right, the downloaded file is also extracted normal.
Time imperceptible to this morning, and department leader together to troubleshoot, first ruled out the problem of Ngnix forwarding output (test a piece of code on the test server), also excludes the problem of generating files part. The code is then more than 10 lines, and finally only the header output to the last ReadFile this part.
ReadFile is to read the file into the buffer, and download the problem, mostly the buffer area before the ReadFile have something. So I bing a "PHP header gz broken" keyword, check this discussion:
Http://stackoverflow.com/questions/22046020/php-downloading-tar-gz-file-increases-file-size-and-changes-md5
It wasn't exactly what I wanted, but it gave me a hint. I found him executing the Ob_clean () before ReadFile, clearing the buffer. This function ensures that the output behind is clean and new. Finally my code is as follows:
$csvFile= '/tmp/downprivilege.csv '; $fp=fopen($csvFile, ' w+ ') or die(' Cant not create file '); fwrite($fp, ' 23233,cw3,232 '); fclose($fp); $tarFile= '/tmp/downprivilege.tar '; $a=NewPhardata ($tarFile); //ADD FILES to Archive.tar FILE $a->addfile ($csvFile, ' Downprivilege.csv '); $a->compress (Phar::GZ); Header("Content-type:application/x-gzip"); $lastPackName=$tarFile. '. Gz '; Header("Accept-ranges:bytes"); Header("Accept-length:".)filesize($lastPackName)); Header("Content-disposition:attachment; Filename=downprivilege.tar.gz "); Header("expires:0"); Header("Cache-control:must-revalidate, post-check=0, pre-check=0"); Header("Cache-control:private"); Header("Pragma:public"); Ob_clean();//clear buffer contents @ReadFile($lastPackName); unlink($tarFile); unlink($csvFile); unlink($lastPackName);
After the download finally correctly can extract something.
Summarize:
1. Do not think that others can help you, especially the less common things, only their own research. I used to think that a team could discuss it together, but few people in the real world would take the time to really discuss it with you.
2. Check the information really do not Baidu, common problems may be wrong answer, and the same. It's still bing+google.
3. The problem should be self-confident, filter out the links do not repeatedly toss.
4. Crooked nuts are often encountered in the non-mainstream situation, can be directly referenced.
Want to have to do similar needs of friends can see this article, because no one in the country can directly answer this question.
Life is a toss: PHP implementation download Export xx.tar.gz