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

Create and edit files?

Discussion in 'Development' started by HittmanA, Nov 21, 2016.

  1. HittmanA

    HittmanA Zombie

    Messages:
    207
    GitHub:
    hittmana
    I am trying to create and then edit a file on and mcpe server from a PocketMine plugin. Unfortunately, my code does not work. Here it is:
    PHP:
    $fp fopen (dirname(__FILE__) . "/foo.zip"'w+');
    $sender->sendMessage($fp);
    The file is not created and no message gets sent. How should I create and edit files with PocketMine, or how should I fix my code?
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    when you use fopen it returns an resource handler not text therefore it should fail
    your console log should say something
    to fix it i would do
    Code:
    $text = fread($handler)
    fclose($handler)
    $sender->send($text)
    
    hopefully it works no clue not tested
     
    HittmanA likes this.
  3. Lambo

    Lambo Spider

    Messages:
    14
    GitHub:
    Lambo16
    fopen returns a resource, not a string. You would need to use stream_get_contents to get the contents of that resource and send that to the player (if it's text).
     
    Last edited: Nov 22, 2016
    SOFe and HittmanA like this.
  4. HittmanA

    HittmanA Zombie

    Messages:
    207
    GitHub:
    hittmana
    Ok but how do I create/edit files? The reading of the file (while wrong) was for debugging purposes.
     
  5. Lambo

    Lambo Spider

    Messages:
    14
    GitHub:
    Lambo16
    PHP:
    $resource fopen($location'w');
    fwrite($resource'some text');
    fclose($resource);
     
    SOFe, Thunder33345 and HittmanA like this.
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    I hope you aren't trying to send the whole binary buffer in the ZIP to your players directly.

    Do you just want to edit a standard file, or edit a file in a zip archive? These are very different.

    Also, you won't want to use "w+". It will overwrite a file, with the support of random file access, which I don't understand why you need. If you just want to make a zip archive, use the ZipArchive class and you don't need to handle any streams directly at all.
     
    Thunder33345 likes this.
  7. HittmanA

    HittmanA Zombie

    Messages:
    207
    GitHub:
    hittmana
    I am not trying to edit a file at all. I simply needed to make one then write to it. I found the solution.
     
  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.