lazynewt Posted May 20, 2008 Share Posted May 20, 2008 i am trying to save info entered into a form to a csv file. The problem im having is i need the csv file to be CREATED using the supplied form info. Heres what im trying. function sendApplicationDataEmail() { $filename = _cleanUp($_REQUEST['Title'])._cleanUp($_REQUEST['FirstNames'])._cleanUp($_REQUEST['LastNames'])'.csv'; $somecontent = _cleanUp($_REQUEST['Title']).","._cleanUp($_REQUEST['FirstNames']).","._cleanUp($_REQUEST['Surname']).","._cleanUp($_REQUEST['DOB'])." ".$lineEnd; // make sure the file exists and is writable first. if (is_writable($filename)) { // open $filename in append mode. if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Thank you for your application"; fclose($handle); } else { echo "The file $filename is not writable"; Its the filename im getting stuck on am i way off or what? the error im getting is that the file is not writteable... thanks in advance ??? Quote Link to comment https://forums.phpfreaks.com/topic/106457-using-filename-create-new-file-with-title-appfirstnames-to-fwrite-to0o/ Share on other sites More sharing options...
jonsjava Posted May 20, 2008 Share Posted May 20, 2008 if you're writing to a linux server, chmod the folder to 666 (read and write ability for everybody) Quote Link to comment https://forums.phpfreaks.com/topic/106457-using-filename-create-new-file-with-title-appfirstnames-to-fwrite-to0o/#findComment-545648 Share on other sites More sharing options...
lazynewt Posted May 20, 2008 Author Share Posted May 20, 2008 Ive tried chmodding ive just checked my code and should i be using fopen w not fopen a ?? Im going to go try that. thanks for the suggestion will try chmoddign again just incase and report back in 5. Quote Link to comment https://forums.phpfreaks.com/topic/106457-using-filename-create-new-file-with-title-appfirstnames-to-fwrite-to0o/#findComment-545650 Share on other sites More sharing options...
jonsjava Posted May 20, 2008 Share Posted May 20, 2008 yes, fopen w is how you write a new file. wow. I somehow missed that Quote Link to comment https://forums.phpfreaks.com/topic/106457-using-filename-create-new-file-with-title-appfirstnames-to-fwrite-to0o/#findComment-545655 Share on other sites More sharing options...
lazynewt Posted May 20, 2008 Author Share Posted May 20, 2008 Hey jonsjava im still having problems. I tried chmodding the folder to 666 but the server doesnt like that and ALL the included files and folders are not accessible via http. im wondering if i could output to a different folder thats chmodded to 666 so the current files/folders are not affected. this would be preferrable because it would collect them all in one place.What change would i need for this? also will this code even work $filename = _cleanUp($_REQUEST['Title'])._cleanUp($_REQUEST['FirstNames'])._cleanUp($_REQUEST['LastNames'])'.csv'; ?????? appreciate the help on this Quote Link to comment https://forums.phpfreaks.com/topic/106457-using-filename-create-new-file-with-title-appfirstnames-to-fwrite-to0o/#findComment-545665 Share on other sites More sharing options...
jonsjava Posted May 20, 2008 Share Posted May 20, 2008 <?php function sendApplicationDataEmail() { $folder = "your_folder/"; /* <-- Change this! */ $filename = _cleanUp($_REQUEST['Title'])._cleanUp($_REQUEST['FirstNames'])._cleanUp($_REQUEST['LastNames'])'.csv'; $somecontent = _cleanUp($_REQUEST['Title']).","._cleanUp($_REQUEST['FirstNames']).","._cleanUp($_REQUEST['Surname']).","._cleanUp($_REQUEST['DOB'])." ".$lineEnd; // make sure the file exists and is writable first. if (is_writable($filename)) { // open $filename in append mode. if (!$handle = fopen($filename, 'w+')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Thank you for your application"; fclose($handle); } else { echo "The file $filename is not writable"; Quote Link to comment https://forums.phpfreaks.com/topic/106457-using-filename-create-new-file-with-title-appfirstnames-to-fwrite-to0o/#findComment-545670 Share on other sites More sharing options...
lazynewt Posted May 20, 2008 Author Share Posted May 20, 2008 Hey jons i really appreciate that know it was a newbie question but im a real nooob at php. I kind of dabble in a few codes... (zencart etc) just when i need to lol. anyway the final code ive just tested and am happy with (using the additional file line you suggested) is. <?php function sendApplicationDataEmail() { $folder = "csv_folder/"; /* <-- Change this! */ $filename = _cleanUp($_REQUEST['Title'])._cleanUp($_REQUEST['FirstNames'])._cleanUp($_REQUEST['LastNames'])'.csv'; $somecontent = _cleanUp($_REQUEST['Title']).","._cleanUp($_REQUEST['FirstNames']).","._cleanUp($_REQUEST['Surname']).","._cleanUp($_REQUEST['DOB'])." ".$lineEnd; // open $filename in write mode. if (!$handle = fopen($filename, 'w')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Thank you for your application"; fclose($handle); } think ive just about removed any redundant code. let me know if you can offer any advice. I appreciate your time jonsjava its helped me alot. Ive been sat here for a few hours now trying to figure this out and have just done it with your help in litterally 10mins lol. cheers Quote Link to comment https://forums.phpfreaks.com/topic/106457-using-filename-create-new-file-with-title-appfirstnames-to-fwrite-to0o/#findComment-545684 Share on other sites More sharing options...
jonsjava Posted May 20, 2008 Share Posted May 20, 2008 It looks like fairly clean code. I can't say that that's the way I'd do it, but every programmer does things their own way. That's what makes PHP so different. 1 million ways to do 1 thing. Just remember, the way you're opening the files, it opens the file and overwrites anything in the file. If the file doesn't exist, attempts to create the file. If you want to append to an existing file, change "w+" to "a". Quote Link to comment https://forums.phpfreaks.com/topic/106457-using-filename-create-new-file-with-title-appfirstnames-to-fwrite-to0o/#findComment-545690 Share on other sites More sharing options...
lazynewt Posted May 20, 2008 Author Share Posted May 20, 2008 Yes im really starting to like php. im also taking an interest in ruby on rails. But im deffo working my way through a few lynda php disks and php on w3schools soon. I think im still having problems getting it to place the csv file in the folder though ill let you know if i get it sorted or if i need to pester you some more. thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/106457-using-filename-create-new-file-with-title-appfirstnames-to-fwrite-to0o/#findComment-545694 Share on other sites More sharing options...
lazynewt Posted May 20, 2008 Author Share Posted May 20, 2008 Yes it seems to be ignoring the $folder hmmmmmmmm Quote Link to comment https://forums.phpfreaks.com/topic/106457-using-filename-create-new-file-with-title-appfirstnames-to-fwrite-to0o/#findComment-545700 Share on other sites More sharing options...
lazynewt Posted May 20, 2008 Author Share Posted May 20, 2008 just wondering if instead of writing the file to "csvfolder" i could use a move command after fclose($handle); .?? ow and sorry for the tripple posts i should of really modified them. Quote Link to comment https://forums.phpfreaks.com/topic/106457-using-filename-create-new-file-with-title-appfirstnames-to-fwrite-to0o/#findComment-545702 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.