Find the answer to your Linux question:
Results 1 to 5 of 5
I am using fputs to print some lines to a file but It doesn't include the end of line character at the end of the string so the strings are ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! errigour's Avatar
    Join Date
    Jan 2009
    Posts
    52

    can someone help me with php file i/o


    I am using fputs to print some lines to a file but It doesn't include the end of line
    character at the end of the string so the strings are all written to the top line.
    how can I add eol to the end of a string printed to a file.
    also how can I read each line of a file by using eol to end the string I want read.

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    fputs() simply prints the string to the file. If the string does not have a newline in it, no newline will be written to the file. So there are two solutions:

    1) Add a newline to the end of the string that you are printing
    2) Explicitly print a newline to the file after the string:
    Code:
    fputs($fp, "\n");
    Reading a line from a file is done by using the fgets() function:
    PHP: fgets - Manual

  3. #3
    Just Joined! hunter_thom's Avatar
    Join Date
    Apr 2010
    Posts
    89
    PHP: stream_get_line - Manual
    PHP: fgets - Manual



    Can you please post the code?

  4. #4
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,482
    Quote Originally Posted by Cabhan View Post
    fputs() simply prints the string to the file. If the string does not have a newline in it, no newline will be written to the file. So there are two solutions:

    1) Add a newline to the end of the string that you are printing
    2) Explicitly print a newline to the file after the string:
    Code:
    fputs($fp, "\n");
    Reading a line from a file is done by using the fgets() function:
    PHP: fgets - Manual
    I'd suggest using the pre-defined constant PHP_EOL instead of '\n' as it is definitely more readable to my eye. If you are developing on Windows and deploying on Linux (or visa versa) then it's pretty much essential.
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    Conkybots: Interactive plugins for your Conkys!

  5. #5
    Just Joined! errigour's Avatar
    Join Date
    Jan 2009
    Posts
    52

    Heres a copy of the code

    While you guys are here is there a more resourceful way too use the fputs that is already being used?
    I was wondering if I could do
    Code:
    fputs($handle, $_POST["username"]."\n");

    Heres a copy of the code
    Code:
    <?php
    session_start();
    if ($_SESSION['id_num'] <= 0)
    {
        $_SESSION['id_num'] = rand();
    }
    
    if (!$_POST["username"])
    {
        echo "<p style=\"color:red;font-weight:bold;margin-left:95px\">";
        echo "* A username is required</p>";
        include("account creation.html");exit;
    }
    else
    {
    /*
        $username         = $_POST["username"];
        $password         = $_POST["password"];
        $passwordvar      = $_POST["passwordvar"];
        $firstname        = $_POST["firstname"];
        $middlename       = $_POST["middlename"];
        $lastname         = $_POST["lastname"];
        $streetaddress    = $_POST["streetaddress"];
        $aptnumber        = $_POST["aptnumber"];
        $streetaddresstwo = $_POST["streetaddresstwo"];
        $city             = $_POST["city"];
        $state            = $_POST["state"];
        $zipcode          = $_POST["zipcode"];         */
    
        if ($_POST["password"] != $_POST["passwordvar"])
        {
            echo "<p style=\"color:red;font-weight:bold;margin-left;95px\">";
            echo "Password's are not the same</p>";
            include("account creation.html");exit;
        }
        else
        {
    
            $syslog = fopen(",/system", "a");
            if(file_exists("database/".$_POST["username"]))
            {
                echo "<p style=\"color:red;font-weight:bold;margin-left:95px\">";
                echo "The username you entered already exists</p>";
                include("account creation.html");exit;
            }
            else
            {
                if (!$handle = fopen("database/".$_POST["username"], "w+"))
                {
                    fputs($syslog, "Unable to open database/".$_POST["username"]);
                    echo "<p style=\"color:red;font-weight:bold;margin-left;95px\">";
                    echo "Password's are not the same</p>";exit;
                }
                else
                {
                    if($_POST["username"]){fputs($handle, $_POST["username"]);}
                    if($_POST["password"]){fputs($handle, $_POST["password"]);}
                    if($_POST["passwordvar"]){fputs($handle, $_POST["passwordvar"]);}
                    if($_POST["firstname"]){fputs($handle, $_POST["middlename"]);}
                    if($_POST["middlename"]){fputs($handle, $_POST["middlename"]);}
                    if($_POST["lastname"]){fputs($handle, $_POST["lastname"]);}
                    if($_POST["streetaddress"]){fputs($handle, $_POST["streetaddress"]);}
                    if($_POST["aptnumber"]){fputs($handle, $_POST["aptnumber"]);}
                    if($_POST["streetaddresstwo"]){fputs($handle, $_POST["streetaddresstwo"]);}
                    if($_POST["city"]){fputs($handle, $_POST["city"]);}
                    if($_POST["state"]){fputs($handle, $_POST["state"]);}
                    if($_POST["zipcode"]){fputs($handle, $_POST["zipcode"]);}
                    fclose($handle);
                }
            }       fclose($syslog);
            include("redirect.php");
            echo "<p style=\"color:red;font-weight:bold;margin-left;95px\">";
            echo "No data has been saved...<br><br>";
            echo "&nbsp; &nbsp;I am currently working on this form, try back<br>";
            echo "tommarrow or the day after and check on my<br>";
            echo "progress.<br>";
            echo "and or email me: eric_justin_allan@cfl.rr.com</p>";
        }
    }
    ?>

Posting Permissions

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