SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Sep 2003
    Location
    Northern California
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    fopen($fh, "a") is not creating the text file

    PHP Code:
    function writeTextFile() {
        
        
    $file "form_results.txt";
        
    $fh fopen($file"a");
        
    $string $_POST['name'].":";
        
    $string .= $_POST['home'].":";
        
    $string .= $_POST['email'].":";
        
    $string .= $_POST['work'].":";
        
    $string .= $_POST['cell'].":";
        
    $string .= $_POST['address'].":";
        
    $string .= $_POST['city_state_zip'].":";
        
    $string .= $_POST['message'].":";
         
    $string .= "\n\r";
         
        
    fwrite($fh$string1000);
        
    fclose($fh);

    //******************************* 
    This code only works when I create an empty text file although I expected the file to be created if it didn't exist (according to php.net doc).

    Also, how do I remove the carriage returns inside <textarea>message with line-breaks </textarea> ? I want the message to be on one line in when it is written to the text file.

  2. #2
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the file not created problem most likely relates to the folder permissions on the folder you are trying to write to... the requirement will vary from server to server so you might as well keep working your way up until the file creation works. Are you getting a specific error?

    i'm not sure if theres a function to remove new lines, maybe you could just use str_replace?
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  3. #3
    SitePoint Guru
    Join Date
    Sep 2003
    Location
    Northern California
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm...the folder is the root directory. not getting any error messages. i can live with this anomaly since creating the file one time is not a hassle.

    regarding the line-breaks/carriage returns, what characters would I search for ?

    this is how the form fields appear when written to the text file (using ":" for delimiter)

    John Doe: (972) 555-1212:emailaddress@yahoo.com:::::asdfak asdf::: Dallas, TX:asdfasdf asdf asdf asfd \'asd
    asdfcxv
    asfd:

  4. #4
    SitePoint Guru
    Join Date
    Sep 2003
    Location
    Northern California
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In case anyone needs to write the contents of a form to a text file and doesn't want any user carriage returns (from inside <textarea></textarea>) to result, use this function. Line breaks that appear in a text file will cause problems if you want to import into Excel, for example;

    PHP Code:
    function nl2br2($string) {
        
    $string str_replace(array("\r\n""\r""\n"), "<br />"$string);
        
    $array explode("<br />"$string);
        foreach (
    $array as $key=>$value){
            
    $newString .= $value;
        }
        return 
    $newString;
    }  
    //****************************** 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •