SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: Php File Write

  1. #1
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Php File Write

    I have this code that outputs information to a textfile but on the firstline it dosnt have a line underneath the text with %#.

    It outputs to the textfile as:

    %#Adult-Spatopic=Adult chatroom 18+ please!, this chatroom is unmoderated.
    category=GN
    language=EN-US
    welcome=Welcome to the adult chat, if you are under the age of 18 please leave.
    owner=ada0d65aw1d8a4w/*-
    host=ad0ad+502a+95wd2a-*8
    modes=nt
    bot=Service_Idle

    and it should be:

    %#Adult-Spa
    topic=Adult chatroom 18+ please!, this chatroom is unmoderated.
    category=GN
    language=EN-US
    welcome=Welcome to the adult chat, if you are under the age of 18 please leave.
    owner=ada0d65aw1d8a4w/*-
    host=ad0ad+502a+95wd2a-*8
    modes=nt
    bot=Service_Idle

    this is my code:

    PHP Code:
    <META HTTP-EQUIV="refresh" CONTENT="30">
    <?php
    function is_empty_folder$folder)
    {
        
    $c 0;
        if ( 
    is_dir($folder))
        {
            
    $files opendir$folder);
            while ( 
    $file readdir($files))
            {
                
    $c++;
            }
            if ( 
    $c 2)
            {
                return 
    false;
            }
            else
            {
                return 
    true;
            }
        }
    }

    $test_folder "c:/New-Chat-Server/rooms";

    if ( 
    is_empty_folder($test_folder))
    {
        echo 
    "No chatrooms found, please turn the chatserver on.";
    }
    else
    {

        echo 
    "Chatrooms found!, everything is running OK.";

        
    $myFile "t:/test.txt";
        
    $fh fopen($myFile'w') or die("can't open file");
        
    $stringData "";
        
    fwrite($fh$stringData);
    fclose($fh);

    function 
    filter_ini($file){
    return (!
    is_dir($file) && preg_match('/(.*)\.ini/'$file));
    }
    $dir 't:/rooms';
    if(!
    file_exists($dir) || !is_dir($dir)){
    die(
    'Folder "'.$dir.'" does not exist');
    }
    if(
    substr($dir, -1) != '/'){
    $dir .= '/';
    }
    $files scandir($dir1);
    if(
    $files === false){
    die(
    'Folder "'.$dir.'" cannot be opened');
    }
    $files array_filter($files'filter_ini');
    foreach(
    $files as $file){
    $lines file($dir $file);
    if(
    $lines !== false && strtolower(trim($lines[10])) == '[perm]'){
    $filename str_replace('.ini'''$file);

    $myFile "t:/test.txt";
    $fh fopen($myFile'a+') or die("can't open file");

    $stringData "$filename";
    fwrite($fh$stringData);
    $stringData "$lines[1]";
    fwrite($fh$stringData);
    $stringData "$lines[2]";
    fwrite($fh$stringData);
    $stringData "$lines[3]";
    fwrite($fh$stringData);
    $stringData "$lines[4]";
    fwrite($fh$stringData);
    $stringData "$lines[6]";
    fwrite($fh$stringData);
    $stringData "$lines[7]";
    fwrite($fh$stringData);
    $lines[9] = preg_replace('/room=(.*?)/''modes=$1'$lines[9]);
    $stringData "$lines[9]";
    fwrite($fh$stringData);
    $stringData "bot=Service_Idle";
    fwrite($fh$stringData);
    fclose($fh);
    }
    }
    }
    ?>
    Animated Chatrooms - www.121chatrooms.net

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    $myFile "t:/test.txt";

    $fh fopen($myFile'a+') or die("can't open file");



    $stringData "$filename"
    Try replacing
    PHP Code:
    $stringData "$filename"
    by
    PHP Code:
    $stringData "$filename"."\n"

    And, just a tip, you can replace

    PHP Code:
    $stringData "$lines[1]";
    fwrite($fh$stringData);
    $stringData "$lines[2]";
    fwrite($fh$stringData);
    $stringData "$lines[3]";
    fwrite($fh$stringData);
    $stringData "$lines[4]";
    fwrite($fh$stringData);
    $stringData "$lines[6]";
    fwrite($fh$stringData);
    $stringData "$lines[7]";
    fwrite($fh$stringData); 
    by

    PHP Code:
    for ($x 1$x 8$x++)
      
    fwrite($fh$lines[$x]); 
    It's shorter and does exactly the same
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX
    And, just a tip, you can replace
    PHP Code:
    $stringData "$lines[1]";
    fwrite($fh$stringData);
    $stringData "$lines[2]";
    fwrite($fh$stringData);
    $stringData "$lines[3]";
    fwrite($fh$stringData);
    $stringData "$lines[4]";
    fwrite($fh$stringData);
    $stringData "$lines[6]";
    fwrite($fh$stringData);
    $stringData "$lines[7]";
    fwrite($fh$stringData); 
    by

    PHP Code:
    for ($x 1$x 8$x++)
      
    fwrite($fh$lines[$x]); 
    It's shorter and does exactly the same
    But he would then use line 5, whereas his original code did not.

    SilverB.

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    But he would then use line 5, whereas his original code did not.

    SilverB.
    Oops I didn't see that...

    PHP Code:
    for ($x 1$x 8$x++)
    {
       if (
    $x != 5fwrite($fh$lines[$x]);

    it would have to be than. Still shorter
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Much better, carry on.

    SilverB.

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Much better, carry on.

    SilverB.
    Alright, here goes:

    PHP Code:
    <META HTTP-EQUIV="refresh" CONTENT="30">
    <?php

    $test_folder 
    "c:/New-Chat-Server/rooms";
    $myFile "t:/test.txt";
    $roomsDir 't:/rooms/';

    function 
    filter_ini($file) {
        return (!
    is_dir($file) && preg_match('/(.*)\.ini/'$file));
    }

    function 
    is_empty_folder($folder) {
        return (
    is_dir($folder) && count(scandir($folder) > 2);
    }

    if (
    is_empty_folder($test_folder)) {
        echo 
    "No chatrooms found, please turn the chatserver on.";
    }
    else {
        echo 
    "Chatrooms found!, everything is running OK.";
        
        if(!
    file_exists($roomsDir) || !is_dir($roomsDir)){
            die(
    'Folder "'.$roomsDir.'" does not exist');
        }
        
        
    $files scandir($roomsDir1);
        if(
    $files === false){
            die(
    'Folder "'.$roomsDir.'" cannot be opened');
        }
        
    $files array_filter($files'filter_ini');
        
        foreach(
    $files as $file){
            
    $lines file($roomsDir $file);
            if(
    $lines !== false && strtolower(trim($lines[10])) == '[perm]'){
                
    $filename str_replace('.ini'''$file);
                
                
    $lines[9] = preg_replace('/room=(.*?)/''modes=$1'$lines[9]);
                
    $fh fopen($myFile'a+') or die("can't open file");
                
                for (
    $x 1$x 10$x++)
                {
                    if (
    $x != && $x != 8)
                    {
                        
    fwrite($fh$lines[$x]);
                    }
                }
                
    fwrite($fh"bot=Service_Idle");
                
    fclose($fh);
            }
        }
    }
    ?>
    What I did:

    - put configurable files/dirs, and functions, at the top of the script
    - rewrote is_empty_dir
    - removed
    if(substr($dir, -1) != '/'){
    $dir .= '/';
    }

    you set the directory yourself, so you can enforce it ends with a /, not need to let PHP check that
    ( I changed the $roomsDir to reflect this. $roomsDir was $dir in the original script, but I found $roomsDir a more descriptive name )

    - removed
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = "";
    fwrite($fh, $stringData);
    fclose($fh);

    Writes an empty string to the file, I have no idea how that would help anything
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol the last big code didnt work blank page and didnt write.
    Animated Chatrooms - www.121chatrooms.net

  8. #8
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm can anyone help
    Animated Chatrooms - www.121chatrooms.net

  9. #9
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Adding an "\n" after "$filename" (so "$filename"."\n") didn't work?
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  10. #10
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no it adds some weird square instead of making it on a new line.
    Animated Chatrooms - www.121chatrooms.net

  11. #11
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Using windows are ya?

    Add "\r\n" instead of just "\n"

    The "\n" is the newline character for *nix
    "\r\n" is for windows
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  12. #12
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it did the same thing

    $stringData = "$filename\r";
    Animated Chatrooms - www.121chatrooms.net

  13. #13
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    $stringData = "$filename\r\n";

    You need both the \r and the \n (stupid windows)
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  14. #14
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sweet all working
    Animated Chatrooms - www.121chatrooms.net

  15. #15
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    If you have a PHP version greater than 5.0.2, you can also do

    $stringData = "$filename".PHP_EOL;

    PHP_EOL is a constant that defines the End Of Line (EOL) character(s).

    For windows it will be \r\n, for *nix \n and for mac \n\r (I believe).

    Advantage of using this constant is that when you run the script on another OS (for example if you go from your windows to a *nix host), you don't have to change every occurrence of \r\n to \n, PHP will automatically work that out for you
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

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