➩ write txt files function code PHP

Technology

➩ write txt files function code PHP

how to write notepad files in PHP and overwrite information in txt


how to write notepad files in PHP and overwrite information in txt

how to write notepad files in PHP and overwrite information in txt


Today I will teach you how to write php files on the same server, can be a paid vps server or using any webmaster tool such as xampp for example, which can execute PHP code, in this mini tutorial we will use as libraries or reference, fwrite , PHP_EOL, File, fopen, and which will perform a function that allows to send by parameters the text, to which we want to record information in a file.




we will call the function create a file which we will pass by a parameter 1 text which we can place the text you want to record or write in the file, text 2 which would be another extra text that you want to show and another parameter that would show errors that would be a boolean if we want to use it later to indicate the function when writing the file and when not to write.



<?php
function createfile($texto1, $texto2,$showfile) {
    if($showfile==1){
    $file = fopen("archivo.txt", "a");

    fwrite($file, "[" . $texto1 . ']' . PHP_EOL);

    fwrite($file, "(" . $texto2 . ')' . PHP_EOL);

    fclose($file);
    }
}
?>


to use it is only to call the create file function, passing through parameters the variables mentioned above, below, I show an example code of how to call it:

<?php

createfile(a first message, a second message,1);

?>

after executing the function in this case a blog of notes .txt will be created with the messages sent by parameters, this file will create it next to the assigned php class or executed, if this code served you please give a like or share with your friend programmer thanks

Post a Comment

0 Comments