Search code examples
phpechofopenfwrite

PHP Script That Creates File Suddenly Stops Working


I made a script to create a file, it was working fine until i think my host changed the script when I asked them what was wrong with it (I fixed it after but now its not working)

Here is the script:

$rand = substr(md5(microtime()),rand(0,26),5);
        $seed = str_split('abcdefghijklmnopqrstuvwxyz'
                 .'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
                 .'0123456789!@#$%^&*()' . 'ǀǁǂǃDŽDždžLJLjljNJNjnjǍǎǏ01D0ǐǑǒǓǔǕǖǗǘǙǚǛǜǝǞǟ01E0ǠǡǢǣǤǥǦǧǨǩǪǫǬǭǮǯ01F0ǰDZDzdzǴǵǶǷǸǹǺǻǼǽǾǿ
0200ȀȁȂȃȄȅȆȇȈȉȊȋȌȍȎȏ0210ȐȑȒȓȔȕȖȗȘșȚțȜȝȞȟ0220ȠȡȢȣȤȥȦȧȨȩȪȫȬȭȮȯ0230ȰȱȲȳȴȵȶȷȸȹȺȻȼȽȾȿ0240ɀɁɂɃɄɅɆɇɈɉɊɋɌɍɎɏ0250ɐɑɒɓɔɕɖɗɘəɚɛɜɝɞɟ0260ɠɡɢɣɤɥɦɧɨɩɪɫɬɭɮɯ0270ɰɱɲɳɴɵɶɷɸɹɺɻɼɽɾɿ0280ʀʁʂʃʄʅʆʇʈʉʊʋʌʍʎʏ0290ʐʑʒʓʔʕʖʗʘʙʚʛʜʝʞʟ02A0ʠʡʢʣʤʥʦʧʨʩʪʫʬʭʮʯ02B0ʰʱʲʳʴʵʶʷʸʹʺʻʼʽʾʿ02C0ˀˁ˂˃˄˅ˆˇˈˉˊˋˌˍˎˏ02D0ːˑ˒˓˔˕˖˗˘˙˚˛˜˝˞˟02E0ˠˡˢˣˤ˥˦˧˨˩˪˫ˬ˭ˮ˯02F0˰˱˲˳˴˵˶˷˸˹˺˻˼˽˾˿0300̀́̂̃̄̅̆̇̈̉̊̋̌̍̎̏0310̛̖̗̘̙̜̝̞̟̐̑̒̓̔̕̚0320̡̢̧̨̠̣̤̥̦̩̪̫̬̭̮̯0330̴̵̶̷̸̰̱̲̳̹̺̻̼̽̾̿0340͇͈͉͍͎̀́͂̓̈́͆͊͋͌ͅ͏0350͓͔͕͖͙͚͐͑͒͗͛͘͜͟͝͞0360ͣͤͥͦͧͨͩͪͫͬͭͮͯ͢͠͡0370ͰͱͲͳʹ͵Ͷͷ͸͹ͺͻͼͽ;Ϳ0380΀΁΂΃΄΅Ά·ΈΉΊ΋Ό΍ΎΏ0390ΐΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟ03A0ΠΡ΢ΣΤΥΦΧΨΩΪΫάέήί03B0ΰαβγδεζηθικλμνξο03C0πρςστυφχψωϊϋόύώϏ03D0ϐϑϒϓϔϕϖϗϘϙϚϛϜϝϞϟ03E0ϠϡϢϣϤϥϦϧϨϩϪϫϬϭϮϯ03F0ϰϱϲϳϴϵ϶ϷϸϹϺϻϼϽϾϿ
'); // and any other characters
shuffle($seed); // probably optional since array_is randomized; this may be redundant
$rand = '';
foreach (array_rand($seed, 50) as $k) $rand .= $seed[$k];
$current = "";

fputs($rand, "[Server]: Welcome To Your New Chat Server");
echo $rand;

Its meant to create a random file and show the user the name of that file

I always get an error:

ec-2017 08:52:43 UTC] PHP Warning: fputs() expects parameter 1 to be resource, string given in /public_html/rechat/nokeep.php


Solution

  • fputs is expecting a resource ( as the error points out) this should be the handle from fopen()...

    $fh = fopen($rand, 'w');
    fputs($fh, "[Server]: Welcome To Your New Chat Server");
    fclose($fh);
    

    You could alternatively use file_put_contents() which would do this all in one step...

    file_put_contents($rand,  "[Server]: Welcome To Your New Chat Server");