Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2007
    Posts
    31

    Exclamation Unanswered: Create php function

    I have a script:



    The problem is the server where i have the page, dont recognize functions file_get_contents() and file_put_contents because is a functio newer then php 4.3.0 and i dont find a server with newer php so i must create a function to take place of my functions. I ve tried something like that. Any ideas..?
    The code:

    <?php
    $fvalori = 'valori3.txt'; // fisierul din care se citesc valorile
    $fcontor = 'contor.txt'; // fisierul in care se pastreaza contorul

    $errbuf = false; // asta o folosim ca sa verificam in diferite puncte daca a aparut vreo eroare

    // verificam sa existe fisierul
    if( !file_exists($fvalori) ){
    // daca nu exista afisam o eroare si $errbuf ia valoarea true (o verificam
    imediat)
    echo 'Fisierul '.$fvalori.' nu exista.<br />';
    $errbuf = true;
    }
    // dupa ce am trecut de codul de mai sus $errbuf va avea valoarea true daca a aparut o eroare care ne impiedica
    // sa folosim fisierul specificat in $fvalori
    // aceleasi verificari le facem si pentru $fcontor

    if( !file_exists($fcontor) ){
    echo 'Fisierul '.$fcontor.' nu exista.<br />';
    }

    if( $errbuf ) die('</body></html>');

    // daca am ajuns aici totul e ok si putem citi din fisier
    // prima data citim valorile
    $valori = file_get_contents($fvalori);
    // apoi citim contorul si folosim trim() ca sa eliminam spatiile de la inceput si sfarsit (in cazul in care exista)
    $contor = trim(file_get_contents($fcontor));
    // verificam daca $contor e numar. daca nu e numar (re)setam contorul la 0
    if( !is_numeric($contor) ) $contor = 0;

    // transformam $valori intr-un array care are in fiecare element continutul unei linii
    // treaba se face cu explode() folosind caracterul de rand nou ca delimitator
    $valori = explode("\n",$valori); // refolosim $valori ca sa nu incarcam inutil memoria. PHP ne permite ;-)

    // avem valorile in array, avem contorul, inainte de afisare mai trebuie sa verificam doar daca contorul nu cumva e mai mare decat numarul de linii din $valori
    // caz in care trebuie sa il resetam la 0
    // se compara cu ($contor+1) pentru ca in PHP numaratoarea incepe de la 0 (C-style)
    if( count($valori) < ($contor+1) ) $contor = 0;

    // acum totul e safe, putem citi linia care ne intereseaza si sa o afisam
    $linie = $valori[$contor]; // linia care urmeaza sa fie afisata e elementul $contor din array-ul $valori
    $linie = explode(' ',$linie);
    for( $i=1 ; $i<count($linie) ; $i++ )
    echo 'Camera '.$i.' este : '.trim($linie[$i]).'<br />';

    file_put_contents($fcontor,++$contor);


    ?>
    The code work at my home, but i have php 5.


    ---------------------------------------------
    <?php
    function file_get_contents()
    {
    $handle = fopen("fisierulmeu.txt", "r");
    while (!feof($handle))
    {
    $buffer = fgets($handle, 512);

    }
    }
    fclose($handle);
    ?>
    ---------------------------------------
    <?php
    function file_put_contents()
    ....
    fputs sau fwrite
    ....
    ?>
    ---------------------------------------
    I need help. Please helm me
    Thanks.

  2. #2
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770

    Post

    Code:
    function file_get_contents($file){
      $buffer = "";
      if(file_exists($file){
        $fh = fopen($file,'r');
        while(!feof($fh)){
          $buffer .= fread($fh,512);
        }
        fclose($fh);
      } else {
        die("FATAL ERROR : file does not exist");
      }
      
      return $buffer;
    }
    Example :

    Code:
    $content = file_get_contents("myfile.txt");

  3. #3
    Join Date
    Apr 2007
    Posts
    31
    Thanks very much..I have a code for file_put_contents, but somethink doesn't work.This is the code:

    function file_put_contentt($handle, $string)
    {
    fwrite($handle, $string);
    }

    function file_get_cont($handle)
    {
    $contents = '';
    while (!feof($handle))
    {
    $contents .= fread($handle, 8192);
    }
    return $contents;

    The code show me a line from valori3.txt and this error:
    Camera 1 este : INCENDIU!
    Camera 2 este : sigura.
    Camera 3 este : sigura.
    Camera 4 este : sigura.
    Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\test\monitincendiu.php on line 27

  4. #4
    Join Date
    Apr 2007
    Posts
    31
    Thanks for help.
    Thats the good code:
    function file_put_contentt($filename, $string)
    {
    $handle = fopen($filename, "w+");
    if ($handle)
    {
    fwrite($handle, $string);
    fclose($handle);
    }

    }

    function file_get_contentt($filename)
    {
    $contents = '';
    $handle = fopen($filename, "r");

    if ($handle)
    {
    while (!feof($handle))
    {
    $contents .= fread($handle, 512);
    }
    fclose($handle);
    }


    return $contents;
    }

Posting Permissions

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