开发者

How to store the contents of an array into a CSV file and save it in a location in php

开发者 https://www.devze.com 2022-12-22 08:43 出处:网络
I have an array which has the contents as the result of an sql query. I have been able to convert them into a CSV as well in the below format

I have an array which has the contents as the result of an sql query. I have been able to convert them into a CSV as well in the below format

2; Testing; IPTV; 9886784; 50061; 28/2/10 09:30:01 AM; 
3; Testing; IPTV; 9886784; 50061; 1/3/10 09:30:01 AM;
4; Testing; IPTV; 9886784; 50061; 2/3/10 09:30:01 AM;
5; Testing; IPTV; 9886784; 50061; 2/3/10 09:30:01 AM;

Now I would like to put this value into a .csv file and save it a particular location.

How can I do the above using php?

Kindly help as I am new to php

Kar开发者_如何转开发tik


how are you constructing your csv?? you can use fputcsv() as you create your csv.eg

$list = array ("2; Testing; IPTV; 9886784; 50061; 28/2/10 09:30:01 AM;");    
$fp = fopen('file.csv', 'w');    
foreach ($list as $line) {
    fputcsv($fp, $line."\n");
}    
fclose($fp);


file_put_contents is your friend, this function (available since PHP5) save the content of a variable to a file.

i.e.

file_put_contents('/tmp/file.csv', $data);


when $data contains your data to be written in a file, then do the following:

<?php
   $filename = "mydata.csv";
   $handle = fopen($filename, "w");
   fwrite($handle, $data);
   fclose($handle);
?>

the second fopen parameter defines, how to open the file. the following values are available:

'r' Open for reading only; place the file pointer at the beginning of the file. 'r+' Open for reading and writing; place the file pointer at the beginning of the file. 'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. 'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.

0

精彩评论

暂无评论...
验证码 换一张
取 消