-2

I have my file php file_put_contents so;

file_put_contents($file, $data . "\n", FILE_APPEND|LOCK_EX);

when it write to my File test.txt , the output is with number of bytes that were written on the file .

a:4:{s:4:"name";s:4:"aaaa";s:5:"email";s:10:"aaa@gmx.de";s:7:"message";s:27:"mm";s:8:"datetime";s:22:"11/10/2018 05:20:35 pm";}

I will that it ONLY write the data WITHOUT bytes number , can please anyone help me , thanks !

my Code;

<?php

header('Content-Type: text/html; Charset=utf-8');
mb_internal_encoding('UTF-8');
date_default_timezone_set('Europe/Berlin');
error_reporting(E_ALL);


class ClassProveContakt3 
{

  private $Name;
  private $Email;
  private $Message;
  private $PostOK;
  private $DateTime;
  private $items; 


   function __construct() 
   {


      $this -> DateTime = date('m/d/Y h:i:s a');  
      $this -> items = ['Name', 'Email', 'Message']; 

      $flag = true;
      foreach ( $this -> items as $key ) {  
                   if ( empty ( $_POST[$key] ) )  {
                     $flag = false;    
                   } else {    
                     $this -> $key = trim( filter_var( $_POST[$key], FILTER_SANITIZE_STRING ) );
                    }    
       }         
     $this -> PostOk = $flag; 
 }





 function ShowForm() 
 {
 ?>
   <form method="POST">
     <label for="name">Name </label>
     <input type="text" id="name" name="Name" value="<?= $this->Name ?>">
     <label for="email">E-mail </label>
     <input type="email" id="email" name="Email" value="<?= $this->Email     

      ?>">
     <br><br>
     <label> Message: <br>
       <textarea cols="45" rows="6" name="Message"><?= $this->Message ?></textarea>      
     </label>
     <br><br>
     <input  type="submit" name="post" value="POST COMMENT" id="comment">
   </form>
 <?php
 }




 function PostOkT() 
 {

   if ($this -> PostOK) 
   {
      return;

   }     
   if (empty($this->Name) ||  empty($this->Email) || empty($this->Message))
   {

     echo "<br>" . "<b>" . "<h3>*** Please enter all required fields ***</h3>" . "</b>";  


   } 
   else 
   {

        $file = "test.txt"; 

        $datetime = date('m/d/Y h:i:s a', time());
        $data = array("name" => $this->Name, "email" => $this->Email, "message" => $this->Message, "datetime" => $datetime);           


        $data = serialize($data);
        file_put_contents($file, $data . "\n", FILE_APPEND|LOCK_EX);

        $messages = file($file);

        foreach ($messages as $value) {
            $data = unserialize($value);


                 echo "<br>"

                       . "<b>From: </b>" . htmlspecialchars( $data["name"])
                       . "<b> at: </b>" . htmlspecialchars( $data["datetime"])
                       . "<br><br>" . htmlspecialchars( $data["email"])                                     
                       . "<br><br>" . htmlspecialchars( $data["message"])
                       . "<br><hr>";

                   }

                }




    }

}       

?>
3edf1w
  • 105
  • 2
  • 9

1 Answers1

0

@ Nigel Ren , I have to do as your tell me...with json.. , I have to try without Json but because I am very new with PHP , I don't know to do it....

Maybe the title of my question , php file_put_contents without bytes written is not the best ....because it is not the responsibility by my bytes number , rather the serialize Function ..... . Maybe an best title would to be , PHP - How display return Value without bytes number and without Json .

Because I have an array in my Code

$data = array("name" => $this->Name, "email" => $this->Email, "message" => $this->Message, "datetime" => $datetime);

should to utilize the Function serialize , therefore I receive the output with number of bytes .

Serialize Function

A PHP array or object or other complex data structure cannot be transported or stored or otherwise used outside of a running PHP script. If you want to persist such a complex data structure beyond a single run of a script, you need to serialize it. That just means to put the structure into a "lower common denominator" that can be handled by things other than PHP, like databases, text files, sockets. The standard PHP function serialize is just a format to express such a thing, it serializes a data structure into a string representation that's unique to PHP and can be reversed into a PHP object using unserialize. There are many other formats though, like JSON or XML.

From here (First Answer..)

So , because I will not receive my return Values together with Number of bytes and I don't know to do only with PHP, I utilize Json .In my Code , I have only to change the else statement , rest from the code stay identical .

else 
{

      $file = "test.txt"; 

      $datetime = date('m/d/Y h:i:s a', time());
      $data = array("name" => $this->Name, "email" => $this->Email, "message" => $this->Message, "datetime" => $datetime);


      $data = json_encode($data);
      file_put_contents($file, $data . "\n", FILE_APPEND|LOCK_EX);


      $messages = file($file);

      foreach ($messages as $value) {
           $data = json_decode($value, true);


                  echo "<br>"

                             . "<b>From: </b>" . htmlspecialchars( $data["name"])
                             . "<b> at: </b>" . htmlspecialchars( $data["datetime"])
                             . "<br><br>" . htmlspecialchars( $data["email"])                                     
                             . "<br><br>" . htmlspecialchars( $data["message"])
                             . "<br><hr>";

      }

}

und now my output from my File test.txt

{"name":"aaaa","email":"aaa@gmx.de","message":"mmmmmmmmmmmmmmmmmmmmmmmmmmm","datetime":"11/12/2018 02:49:46 pm"}

3edf1w
  • 105
  • 2
  • 9