2014-12-30 11 views
10

Tôi có yêu cầu tạo 3 tệp CSV (trong bộ nhớ) trong một yêu cầu HTTP, nén tệp vào một tệp nén và trả về tệp nén dưới dạng phản hồi HTTP.PHP Tạo nhiều tệp CSV trong bộ nhớ rồi nén

Tôi có đoạn code sau để tạo file zip ...

$files = array($file1, $file2); 
$zipname = 'file.zip'; 
$zip = new ZipArchive; 
$zip->open($zipname, ZipArchive::CREATE); 
foreach ($files as $file) { 
    $zip->addFile($file); 
} 
$zip->close(); 

header('Content-Type: application/zip'); 
header('Content-disposition: attachment; filename='.$zipname); 
header('Content-Length: ' . filesize($zipname)); 
readfile($zipname); 

Tuy nhiên, tôi không biết làm thế nào để tạo ra các tập tin CSV trong bộ nhớ.

Tôi làm cách nào để đạt được điều này?

Trả lời

14

Hãy thử này ...

// some data to be used in the csv files 
$headers = array('id', 'name', 'age', 'species'); 
$records = array(
    array('1', 'gise', '4', 'cat'), 
    array('2', 'hek2mgl', '36', 'human') 
); 

// create your zip file 
$zipname = 'file.zip'; 
$zip = new ZipArchive; 
$zip->open($zipname, ZipArchive::CREATE); 

// loop to create 3 csv files 
for ($i = 0; $i < 3; $i++) { 

    // create a temporary file 
    $fd = fopen('php://temp/maxmemory:1048576', 'w'); 
    if (false === $fd) { 
     die('Failed to create temporary file'); 
    } 

    // write the data to csv 
    fputcsv($fd, $headers); 
    foreach($records as $record) { 
     fputcsv($fd, $record); 
    } 

    // return to the start of the stream 
    rewind($fd); 

    // add the in-memory file to the archive, giving a name 
    $zip->addFromString('file-'.$i.'.csv', stream_get_contents($fd)); 
    //close the file 
    fclose($fd); 
} 
// close the archive 
$zip->close(); 


header('Content-Type: application/zip'); 
header('Content-disposition: attachment; filename='.$zipname); 
header('Content-Length: ' . filesize($zipname)); 
readfile($zipname); 

// remove the zip archive 
// you could also use the temp file method above for this. 
unlink($zipname); 

Tôi vừa thử nghiệm điều này trên máy tính của tôi và nó hoạt động tốt.

Tôi đã sử dụng liên kết này làm tài liệu tham khảo, Nó có thể hữu ích.

MetaShock Reference

+0

Rất hữu ích. Cảm ơn rất nhiều. – Cloud

1

Bạn có thể sử dụng php của memory wrapper:

$zipname = 'php://memory'; 

Trên hệ thống có /dev/shm hệ thống tập tin, bạn có thể tạo ra các file đó, họ sẽ được giữ chỉ trong bộ nhớ và chỉ có thể truy cập đến quá trình hiện hành. Đừng quên xóa chúng sau khi gửi, quá trình máy chủ web sẽ tiếp tục chạy.

+0

Điều này có hỗ trợ nhiều tệp không? Ví dụ ... $ zipname1 = 'php: // memory'; $ zipname2 = 'php: // memory'; $ zipname3 = 'php: // bộ nhớ'; – fml

+0

Mỗi 'fopen ('php: // memory', $ mode)' sẽ mở riêng biệt trong tập tin moemory. Vấn đề là họ không được đặt tên, đóng con trỏ tập tin sẽ mất những gì đã được viết. Tôi không biết 'ZipArchive' thư viện để biết nếu đây là một vấn đề, có khả năng bạn không nên gọi' $ zip-> close() 'nhưng cái gì khác để có được dòng. Có một giải pháp khác, tôi sẽ cập nhật câu trả lời của mình. – Marek

Các vấn đề liên quan