Create a CSV file in php -
I want to create a new CSV file with some values in it and my upload directory. Can anyone guide me how I can create a CSV and what code should be written in PHP, besides, I want the CSV file with the input parameter of the form and the current date. Example: It would also be great if you can advise me how I can define a column header. Example: See this as an example, first off, definitely You need a form from: Sample Form: Then process it in PHP: Note: To create / write that particular path you may need to set your permissions on that PHP < / Blockquote> Test_24-04-2014.csv .
------------------------------- --------- Senior No Name ------------------------------------- ---
& lt ;! - HTML - & gt; & Lt; Form method = "POST" verb = "processes_the_form.php" & gt; Senior Number: & lt; Input type = "text" name = "srno" /> gt; & Lt; Br / & gt; Name: & lt; Input type = "text" name = "name" /> gt; & Lt; Br / & gt; Invt.: & Lt; Input type = "text" name = "invt" /> & Lt; Br / & gt; & Lt; Input type = "submit" name = "submit" value = "add to CSV" /> & Lt; / Form & gt;
& lt ;? Php // set delimitter (tab or comma) $ delimitter = chr (9); // This tab is $ NEWLINE; "\ R \ n"; // Submit the formatting form // if you want to // if you want (isset ($ _ POST ['submit']) {// Value collected $ srno = $ _POST ['srno']; $ Name = $ _POST ['name']; $ Invt = $ _POST ['invt']; // check for csv file $ directory = ''; // sample: path / to / csv / $ prefix = 'test_'; $ Date = date ('D-M-Y'); $ File_path = $ directory $ prefix $ date '.csv'; // Initial build if (! File_exists ($ file_path)) {$ file = fopen ($ file_path, 'w'); // header $ header = add array ('SrNo', 'name', 'invt'); Fputcsv ($ file, $ header, $ delimiter); Fclose ($ file); } $ Formatted_data = array ($ srno, $ name, $ invt); // put them inside the file and append it to $ file = fopen ($ file_path, 'a'); Fputcsv ($ file, $ formatted_data, $ delimitter); Fclose ($ file); }? & Gt;
Comments
Post a Comment