2012-05-23

Java 腦袋學 PHP File

輸出 CSV 檔案
$h = fopen('csv.csv', 'w') or die("Can't open csv.csv");
foreach ($arrays as $array) {
fputcsv($h, $array);
}
fclose($h) or die("Can't close csv.csv");
csv.csv 檔案位置同該 php 檔案所在位置。


輸出 CSV 到畫面
$h = fopen('php://output', 'w') or die("Can't open php://output");
foreach ($arrays as $array) {
fputcsv($h, $array);

}
fclose($h) or die("Can't closephp://output");

輸出 CSV 到暫存
ob_start();
$h = fopen('php://output', 'w') or die("Can't open php://output");
foreach ($arrays as $array) {
fputcsv($h, $array);

}
fclose($h) or die("Can't closephp://output");
$output = ob_get_contents();
ob_end_clean();
讀取一般檔案
$h = fopen('csv.csv', 'r') or die("Can't open csv.csv");
while ($string = fgets($h, 1024)) {
...
}
fclose($h) or die("Can't close csv.csv");
超快讀取一般檔案
$s = file_get_contents('csv.csv');
讀取 CSV 檔案
$h = fopen('csv.csv', 'r') or die("Can't open csv.csv");
while ($array = fgetcsv($h)) {
...
}
fclose($h) or die("Can't close csv.csv");

沒有留言:

張貼留言