RandolphWillems Posted December 18, 2014 Share Posted December 18, 2014 <?php if(isset($_POST['submit'])){ //collect form data $location = $_POST['location']; $ID = $_POST['ID']; $section = $_POST['section']; //check name is set if($location ==''){ $error[] = 'Name is required'; } //if no errors carry on if(!isset($error)){ # Title of the CSV $Content = "location, ID, section\n"; //set the data of the CSV $Content .= "$location, $ID, $section\n"; # set the file name and create CSV file $my_file = ("$location$ID$section.cvs"); $handle = fopen("$my_file", "w") or die('Cannot open file: '.$my_file); header('Content-Type: application/csv'); header('Content-Disposition: attachment; filename="' . $FileName . '"'); echo $Content; exit(); } } //if their are errors display them if(isset($error)){ foreach($error as $error){ echo "<p style='color:#ff0000'>$error</p>"; } } ?> <form action='' method='post'> <p><label>Location:</label><br><input type='text' name='location' value=''></p> <p><label>ID:</label><br><input type='text' name='ID' value=''></p> <p><label>Section:</label><br><input type='text' name='section' value=''></p> <p><input type='submit' name='submit' value='Submit'></p> </form> I have wrote a .php file which is a form with 3 different fields, in these fields I want to write entries which lateron will be submitted into a .csv generating a csv file on my server with the name of the entries. The issue is that the file is not being generated on my server. With the current code the entries are being recognized and being placed in the filename of the csv. The point is not to make it downloadable sinds I want to have it on my webserver and keep editing information in it. My code is top of this post. My .php file replies that: Cannot open file: BOD10Buffer.csv Pardon my sloppy code everyone, I am not really a person that codes but this "form" may save me a lot of time in the longrun, just difficult and I am asking for help on this. Is there anyone that may be kind enough to help me? and as to what I might be doing wrong? All help is much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/293169-need-help-generate-csv-on-server/ Share on other sites More sharing options...
Ch0cu3r Posted December 18, 2014 Share Posted December 18, 2014 The point is not to make it downloadable Then remove these lines header('Content-Type: application/csv'); header('Content-Disposition: attachment; filename="' . $FileName . '"'); echo $Content; exit(); Those lines will force the file to be downloaded. To write your data to a csv file then look into using fputcsv Quote Link to comment https://forums.phpfreaks.com/topic/293169-need-help-generate-csv-on-server/#findComment-1500020 Share on other sites More sharing options...
hansford Posted December 19, 2014 Share Posted December 19, 2014 (edited) There's a lot of in's and out's so I'm just going to illustrate basic handling. Don't think of this code as "production code" - I don't even know if it works lmao! <?php $error = array(); if (isset($_POST['submit'])){ // create an array to input data in each csv row $row = array(); // assuming (from code presented) // you only need to check that location isset and not an empty string if (isset($_POST['location']) && $_POST['location'] != '') { //collect form data $row[] = $location = $_POST['location']; $row[] = $ID = $_POST['ID']; // $row[] = $section = $_POST['section']; } else { $error[] = 'Name is required'; } // if you need to check more $_POST vars - do it here // just edit the above code to not include that $_POST var in the $fields[] array if (isset($_POST['ID']) && $_POST['ID'] != '') { $row[] = $ID = $_POST['ID']; } else { $error[] = 'ID is required'; } // if errors - display errors and stop the code here and now if ( ! empty($error) ) { // for dubugging this is fine, but think 'user friendly' when in production // display errors foreach ($error as $err) { echo "<p style='color:#ff0000;'>$err</p>"; exit(); } } // You create the path and filename // don't rely on stringing together user input to do it for you // fopen() could choke // You don't want spaces and bs chars in your path and filename $my_file = 'whatever_path/' . 'whatever_file_name.csv'; // watch it here - if file already exists, you could overwrite existing data $handle = @fopen($my_file, "w"); if ( ! $handle) { // do error handling - file failed to create // die() during debugging is fine, but be more gentle on users in production } // write data to csv file // you only have one line to write, but may have more later on - just loop it foreach ($list as $fields) { // need some delimter that tells the code - hey! we are starting a new column // look up fputcsv() as suggested by the senior member, Ch0cu3r. fputcsv($handle, $fields, $delimeter); } fclose($handle); } Edited December 19, 2014 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/293169-need-help-generate-csv-on-server/#findComment-1500047 Share on other sites More sharing options...
RandolphWillems Posted December 19, 2014 Author Share Posted December 19, 2014 With the help of this post and another friend explaining me in detail, we all managed to get it working , as a reference I will post the final code and is available for those that need it, passing on the knowledge as well . I thank you Ch0cu3r and Hansford, you guys have been great help. <?php if(isset($_POST['submit'])){ //collect form data $location = $_POST['location']; $ID = $_POST['ID']; $section = $_POST['section']; //check name is set if($location ==''){ $error[] = 'Name is required'; } //if no errors carry on if(!isset($error)){ $list = array( 'location','ID','section', $location, $ID, $section, ); # set the file name and create CSV file $my_file_name = ("$location$ID$section.cvs"); $file = fopen("$my_file_name", "w"); foreach($list as $line){ fputcsv($file,explode(',',$line),','); } fclose($file); } } //if their are errors display them if(isset($error)){ foreach($error as $error){ echo "<p style='color:#ff0000'>$error</p>"; } } ?> <form action='' method='post'> <p><label>Location:</label><br><input type='text' name='location' value=''></p> <p><label>ID:</label><br><input type='text' name='ID' value=''></p> <p><label>Section:</label><br><input type='text' name='section' value=''></p> <p><input type='submit' name='submit' value='Submit'></p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/293169-need-help-generate-csv-on-server/#findComment-1500082 Share on other sites More sharing options...
ginerjm Posted December 19, 2014 Share Posted December 19, 2014 You're happy with this code? Really? To me - it doesn't do anything that you started out to do. 1 - You name the file ".cvs". I thought you wanted a csv file? 2 - You write out error messages using a foreach statement that surprisingly works even tho it doesn't read very well. For future work try doing it this way instead of using a statement that is difficult to comprehend: foreach ($error as $msg) echo $msg; using "$error as $error" reads poorly and while it works it will cause endless concern to anyone reading the code. Plus - why even bother with an array of error messages when you only have one error condition?? 3 - You build an array out of your output values with an extra "empty" element. Why? 4 - You output each element of your array as a new line in your (supposed) csv file. Why do you call it a csv file when there isn't a comma to be found in it and there is no structure to the file that allows it to be treated as a csv file would normally be treated? 5 - Lastly - you build an array of the 6 (7?) values you want to output. Then you iterate thru each ONE of them and write code that is supposed to create an array out of each of them (huh?) and pass that to the fputcsv call. Do you think before you write code, or is this just something you copied from somewhere? And why are you happy with a (supposed) .csv file that is simply a list of 7 lines with one piece of data on each? All in all this is a seriously confounding piece of code. Sure it does something, but what and why is beyond me. Quote Link to comment https://forums.phpfreaks.com/topic/293169-need-help-generate-csv-on-server/#findComment-1500087 Share on other sites More sharing options...
Barand Posted December 19, 2014 Share Posted December 19, 2014 Why do you call it a csv file when there isn't a comma to be found in it and there is no structure to the file that allows it to be treated as a csv file would normally be treated? ginerjm, fputcsv($file,explode(',',$line),','); | | | FYI, that is a comma Quote Link to comment https://forums.phpfreaks.com/topic/293169-need-help-generate-csv-on-server/#findComment-1500093 Share on other sites More sharing options...
ginerjm Posted December 19, 2014 Share Posted December 19, 2014 Barand, And what are you telling me? Adding a comma as a "field delimiter" for an output line that consists of a single field doesn't generate a comma in the output. At least it didn't in my test of the OP's code. Quote Link to comment https://forums.phpfreaks.com/topic/293169-need-help-generate-csv-on-server/#findComment-1500096 Share on other sites More sharing options...
Barand Posted December 19, 2014 Share Posted December 19, 2014 I see what you mean. I had a closer look at his array. I saw what I expected to see last time. $list = array( 'location', 'ID', 'section', $location, $ID, $section ); Quote Link to comment https://forums.phpfreaks.com/topic/293169-need-help-generate-csv-on-server/#findComment-1500098 Share on other sites More sharing options...
ginerjm Posted December 19, 2014 Share Posted December 19, 2014 It appears to me that the OP copied this code from (perhaps) multiple places and doesn't have a clue what it is he is running. Quote Link to comment https://forums.phpfreaks.com/topic/293169-need-help-generate-csv-on-server/#findComment-1500104 Share on other sites More sharing options...
hansford Posted December 20, 2014 Share Posted December 20, 2014 (edited) I ran my code, fixed some issues. So, this example works on a live server. Need to remember to give write privileges to the file and directory. <?php $error = array(); if (isset($_POST['submit'])){ // create an array to input data in each csv row $row = array(); // assuming (from code presented) // you only need to check that location isset and not an empty string if (isset($_POST['location']) && $_POST['location'] != '') { //collect form data $row[] = $location = $_POST['location']; $row[] = $ID = $_POST['ID']; $row[] = $section = $_POST['section']; } else { $error[] = 'Name is required'; } // if you need to check more $_POST vars - do it here;same as above. // just edit the above code to not include that $_POST var in the $row[] array if ( ! empty($error) ) { // for dubugging this is fine, but think 'user friendly' when in production // display errors foreach ($error as $err) { echo "<p style='color:#ff0000;'>$err</p>"; } exit(); } // You create the path and filename // don't rely on stringing together user input to do it for you // fopen() could choke // You don't want spaces and bs chars in your path and filename //$my_file = 'whatever_path/' . 'whatever_file_name.csv'; $my_file = $_SERVER['DOCUMENT_ROOT'] . '/phpfreaks/' . 'test.csv'; // using 'a' since it won't overwrite an existing file name's content, but append to it // it will still create it if it doesn't exist. $handle = @fopen($my_file, "a"); if ( ! $handle) { // do error handling - file failed to create/open for appending // killing execution during debugging is fine, but // be more gentle on users in production echo 'failed to open file'; exit(); } // write line to csv file if ((fputcsv($handle, $row)) === false) { echo 'failed to write csv file.'; } // close file fclose($handle); } ?> Edited December 20, 2014 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/293169-need-help-generate-csv-on-server/#findComment-1500123 Share on other sites More sharing options...
ginerjm Posted December 20, 2014 Share Posted December 20, 2014 (edited) The OP has already said "he has code that works" and that he's happy, despite several incredulous responses Edited December 20, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/293169-need-help-generate-csv-on-server/#findComment-1500138 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.