当前位置:网站首页>About CI framework batch export to compressed file

About CI framework batch export to compressed file

2022-06-11 10:21:00 cathy1213

Recently received a request , Many files need to be exported as compressed files , At first, I thought it was necessary to create directories layer by layer , Put the corresponding file in , Then use the compression class to compress the large folder as a whole , Later, it was found that it was not so complicated ,CI Of read_file Method to directly read the file into the specified location , then download After downloading, it is the hierarchy you want .

There is a problem after practice , Alibaba cloud oss Can't write directly to a compressed file , Maybe I can't read it , Because local files can be written directly . I can't find a solution online , Think about it. , Simply write the file locally first , Then write the compressed file locally , Here comes the question , frequently-used file_get_contents and file_put_contents There's no way oss Write the file locally , Maybe I'm not good at it , Use it in a different position curl Read , succeed , Problem solving . Here's how ... I didn't go into it , If there is a great God to help me answer the reason , Thank you very much .

$cur_encoding = iconv_get_encoding();		// Get current iconv The coding , Convenient for later conversion 
$this->load->library('zip');
foreach ($attach_file as $v){
    $extend2 = strtolower(strrchr($v['url'], '.'));	// To obtain the suffix 
    //--------- Use curl Write files locally 
    $ch = curl_init($v['url']);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $rawdata = curl_exec ($ch);
    curl_close ($ch);
    $fp = fopen(APPPATH.'cache/tmpdocx/a'.$extend2,'w');	//fopen The first parameter of , If you specify a directory , If the directory does not exist , Will not write successfully . You can also put it directly under the root directory , Anyway, this file is just a temporary file , It will still be deleted after the compressed file is written .
    fwrite($fp, $rawdata);
    fclose($fp);
    //---------- Write completed .
    $dir_name = $dir_title.'/ Mission statement /';
    $dir_name = iconv ( $cur_encoding['input_encoding'], 'GBK', $dir_name);	// Here is to ensure that the file name encoding supports Chinese , use iconv To GBK
    $file_name = iconv($cur_encoding['input_encoding'],'GBK',$v['name']);
    // use read_file Write compressed asking price , The first parameter is the use of curl Local file written , The second parameter is the target location , Support hierarchy , That is, the corresponding directory level in the compressed file .
    $this->zip->read_file(APPPATH.'cache/tmpdocx/a'.$extend2,$dir_name.$file_name);
    // Delete curl Temporary files generated .
    unlink(APPPATH.'cache/tmpdocx/a'.$extend2);
}
// After all files are written to the compressed file , export , The browser will automatically download .
$this->zip->download(' Information .zip');
原网站

版权声明
本文为[cathy1213]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110920099153.html