1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

How to write in a .txt file

Discussion in 'Development' started by Levi, Jun 25, 2018.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    How can i put stuff to a .txt file from a plugin
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Assuming you've decided you don't want to use the simpler config defaults with pocketmine, you can do it using just PHP. You don't need PM to create/write in files. This link is a good resource on creating and writing to files.

    If you wanted to do it involving Pocketmine API methods, check out JackNoordhuis' Config tutorial. The Config class supports multiple file types, so you can use txt instead of YAML.
     
    HimbeersaftLP likes this.
  3. Nawaf1b

    Nawaf1b Silverfish

    Messages:
    17
    Code:
    $myfile = fopen("$filename.txt", "w") or die("File not found");
    fwrite($myfile, $txt);
    fclose($myfile);
     
    Last edited: Jun 25, 2018
  4. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    how do i get fopen the .txt file in my plugin's data folder
     
  5. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Assuming you are in your plugin's main file, you can use $this->getDataFolder() to return the resource folder for your plugin, I believe.
     
  6. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    yes i did but it doesnt write anything to the txt file
     
  7. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    What exact code did you use to write to the file? @Nawaf1b's code seems correct to me.
     
  8. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    PHP:
    $myfile fopen(MainClass::getInstance()->getDataFolder() . "test.txt""w") or die("Unable to open file!");
    $txt "hi this test";
    fwrite($myfile$txt);
    fclose($myfile);
     
  9. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Any error or output to the console? Have you tried a simple echo statement in the same block to see if the code is being run at all?
     
  10. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
  11. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    PHP:
    in commandsPreproccessevent

            file_put_contents
    ($plugin->getDataFolder()."logger.txt",$event->getMessage()."\n");
    it doesnt write new lines
     
  12. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    ???
     
  13. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    it writes only latest typed command
     
  14. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Use file_put_contents("somefile.txt", "somedata", FILE_APPEND); :D
     
    HimbeersaftLP and corytortoise like this.
  15. Bamuel

    Bamuel Silverfish

    Messages:
    16
    GitHub:
    bamuel
    upload_2018-7-2_13-10-58.png
    There are different modes in fopen in php.
    Code:
    fopen("file.txt", "w+");
    would be the best option todo in your case.

    if you want it to write at the top of the text file or if you want it to erase text file and create a new one
    Code:
    unlink('file.txt');
    the w+ mode will create a new file every time
     
  16. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    You don't need + if you don't read from the same stream.
     
    Bamuel likes this.
  17. Bamuel

    Bamuel Silverfish

    Messages:
    16
    GitHub:
    bamuel
    w: write only, *
    w+: read/write, *

    fopen with w letter denotes that text is appended to the end of the file, while w+ deletes the content of the file and starts writing new content into the file.

    I was assuming that he would do something extreme with his code that he would require w+ and it will create the same output, even though it will take longer (in super small time) to generate
     
  18. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    The + sign is generally not needed unless you need to use fseek() somewhere.
     
    Bamuel likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.