file_put_contents() for PHP4!

July 30th, 2007 by Quatrux

When I was using PHP4 for my web stuff, I wrote a quite good function for usage with PHP4, as a lot of you know on PHP5 a very handy function file_put_contents(); exist, but not in PHP4, but if you want to use it on PHP4, due to your server still has it running, which in my opinion is still quite normal, I will share another of my functions, I just browsed around my sources which usually never is finished and found it, so thought to share, it is in my opinion quite different when the one offered on the PEAR package, but it does the same, just is a little bit expanded.. I will explain a little! Firstly I named it put_file_contents(); to avoid the risk of duplication, but for instance, just do a check with the simple function function_exists(); Moreover, I recommend to include your functions only once, I mean use include_once() and/or require_once() ;)

  1. function put_file_contents($file, $data, $mode = ‘a’, $path = ‘0′, $y = ) {
  2.         /* check if file exists and atempt to Create it */
  3.         if (!file_exists($file) && !touch($file)) {
  4.                 user_error(‘put_file_contents() Can not create the file’, E_USER_WARNING);
  5.                 return FALSE;
  6.         /* check if Directory exists */
  7.         } elseif (is_dir($file)) {
  8.                 user_error(‘put_file_contents() Can not write to a directory’, E_USER_WARNING);
  9.                 return FALSE;
  10.         /* check if file is regular */
  11.         } elseif (is_file($file)) {
  12.                 /* check the Flags */
  13.                 if (!strpos(‘**|a|w|t|’, ‘|’.$mode.‘|’)) {
  14.                         $mode = ‘a’;
  15.                 /* Change Owner if required */
  16.                 } elseif ($mode == ‘t’ AND !qcreate_file($file)) {
  17.                         user_error(‘put_file_contents() Can not change file owner’, E_USER_WARNING);
  18.                         return FALSE;
  19.                 }
  20.                 /* check if file is writabl/stronge otherwise try to change permsissions */
  21.                 if (!is_writable($file) && (!chmod($file, 0666) OR !qftp_chmod($file))) {
  22.                         user_error(‘put_file_contents() Can not write to file, permission denied’, E_USER_WARNING);
  23.                         return FALSE;
  24.                 /* Change Data Array to String */
  25.                 } elseif (is_array($data)) {
  26.                         $data = implode($y, $data);
  27.                 /* Check Input Data */
  28.                 } elseif (!is_scalar($data)) {
  29.                         user_error(‘put_file_contents() The 2nd parameter should be either a string or an array’, E_USER_WARNING);
  30.                         return FALSE;
  31.                 }
  32.                 /* Open and Store Data to File */
  33.                 if (($fo = fopen($file, $mode, $path)) === FALSE) {
  34.                         user_error(‘put_file_contents() Failed to open stream: Unknown reason’, E_USER_WARNING);
  35.                         return FALSE;
  36.                 }
  37.                 if (fputs($fo, $data) === FALSE) {
  38.                         user_error(‘put_file_contents() Failed to write to file: Unknown reason’, E_USER_WARNING);
  39.                         return FALSE;
  40.                 }
  41.                 /* Close Handle */
  42.                 fclose($fo);
  43.                 return TRUE;
  44.         } else {
  45.                 user_error(‘put_file_contents() Can not continue: Not a file’, E_USER_WARNING);
  46.                 return FALSE;
  47.         }
  48. }

As you can see, several times more of my stupid custom functions were used, you can remove them, but I will still share them, even though I don’t think they are useful.. I just tried to paste them, but I saw even more custom things :D So I think I will only leave and idea and not an actually working code.

Posted in PHP | 1 Comment »

Leave a Comment



Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment and don't be afraid of the spam protection!