2014-12-30 4 views
10

संपीड़ित करें मुझे एक HTTP अनुरोध के दौरान 3 सीएसवी फाइलें (मेमोरी में) बनाने की आवश्यकता है, फ़ाइलों को एक संपीड़ित फ़ाइल में ज़िप करें और संपीड़ित फ़ाइल को HTTP प्रतिक्रिया के रूप में वापस करें।PHP मेमोरी में एकाधिक सीएसवी फाइलें बनाएं तो

मैं ज़िप फ़ाइल बनाने के लिए निम्न कोड है ...

$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); 

हालांकि, मैं स्मृति में CSV फ़ाइलों को बनाने के लिए कैसे पता नहीं है।

मैं इसे कैसे प्राप्त कर सकता हूं?

उत्तर

14

इस प्रयास करें ...

// 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); 

मैं सिर्फ अपनी मशीन पर यह परीक्षण किया है और यह ठीक काम करता है।

मैंने इस लिंक का संदर्भ के रूप में उपयोग किया, यह उपयोगी हो सकता है।

MetaShock Reference

+0

बहुत उपयोगी। बहुत बहुत धन्यवाद। – Cloud

1

आप उपयोग कर सकते हैं php के memory wrapper:

$zipname = 'php://memory'; 

सिस्टम /dev/shm फाइल सिस्टम तुम वहाँ फ़ाइलें बना सकते हैं होने पर, वे केवल स्मृति में और केवल वर्तमान प्रक्रिया के लिए सुलभ रखा जाएगा। भेजने के बाद उन्हें हटाने के लिए मत भूलना, वेब सर्वर प्रक्रिया चलती रहेगी।

+0

क्या यह एकाधिक फ़ाइलों का समर्थन करेगा? उदाहरण के लिए ... $ zipname1 = 'php: // memory'; $ zipname2 = 'php: // memory'; $ zipname3 = 'php: // memory'; – fml

+0

प्रत्येक 'fopen (' php: // memory ', $ mode)' moemory फ़ाइल में अलग खुल जाएगा। समस्या यह है कि उनका नाम नहीं दिया गया है, फाइल पॉइंटर को बंद करने से जो लिखा गया था उसे खो देंगे। मुझे यह जानने के लिए 'ज़िप आर्चिव' लाइब्रेरी नहीं पता है कि यह एक समस्या है, शायद आपको '$ ज़िप-> क्लोज़()' पर कॉल नहीं करना चाहिए, लेकिन स्ट्रीम प्राप्त करने के लिए कुछ और नहीं। अभी तक एक और समाधान है, मैं अपना जवाब अपडेट करूंगा। – Marek

संबंधित मुद्दे