Can not create text file with php -
i'm new php. i'm trying write small script writes small text file , reads same file , show contents on screen. script dies, , error message "error: can not create file!"
i'm running code in home centos sandbox.
<?php ////////////////////////////////////////////////////////////// // http://inpics.net/tutorials/php/files3.html // php date function: // http://www.w3schools.com/php/func_date_date.asp // date("d/m/y h:i:s") ////////////////////////////////////////////////////////////// // define filename $myfilename = (date("dmy").".txt"); print "<p>filename: '<b>$myfilename</b>'</p>"; // define text $mytext = 'some text... blah, blah, blah...'; print "<p>this text should on file:'<b>$mytext</b>'</p>"; ////////////////////////////////////////////////////////////// // open file write: // $handle = fopen($myfilename, 'w') or die('error: can not create file!'); fwrite($handle,$mytext,2048); fclose ($handle); ////////////////////////////////////////////////////////////// // code below works, following example: // http://stackoverflow.com/a/9861638/4321334 $handle = fopen($myfilename, 'r') or die('error: can not open file!'); print "<p>now, let's read file: <b>$myfilename</b> </p>"; $filecontents = fread($handle,filesize($myfilename)); fclose ($handle); print "<p>the file <b>'$myfilename'</b> have following text on it:</p>"; print "<p>$filecontents</p>"; print "<p>happy ending!</p>"; exit; ?>
thanks in advance.
i suggest using file_put_contents
, achieve similar goal, wrapper fwrite
file_put_contents($myfilename, $mytext);
Comments
Post a Comment