Does anyone have a good working example of how to perform file_put_contents() equivalent for PHP 4+? Upgrading to PHP 5+ is absolutely, positively, unequivocably never going to happen at this environment..

So what do I do? I wrote my own version, but if the file does not [yet] exist, it bombs. Every online example I find is the same way, yet isn't file_put_contents() supposed to create the file if it does not yet exist? If so, how do I implement it?

Here's what I have so far:

if (!function_exists('file_put_contents')) {
 /**
  * Place content into file
  * 
  * *NOTE* This function will also utilize is_binary() to determine if the file is to be binary and will handle accordingly
  *
  * @access public
  * @param string $filePath
  * @param mixed $data
  * @param int $flags (optional)
  * @return int Returns {@link http://us2.php.net/manual/en/function.filesize.php filesize()} of file or returns false to mirror behavior of {@link http://us2.php.net/manual/en/function.file-put-contents.php file_put_contents()} in PHP 5+
  * @link http://us2.php.net/manual/en/function.file-put-contents.php Click here for information on file_put_contents()
  * @see is_binary
  * @see actual_path
  */
 function file_put_contents($filePath, $data, $flags = '') {
	$flags = (FLAG_APPEND || 'a' || 'A' || 'a+' || 'A+') ? 'a+' : 'w+';
	if (is_binary($filePath)) $flags = preg_replace('/^([^\+])+\+$/', '$1b+', $flags); // BINARY APPEND OR WRITE
	$fileID = @fopen(actual_path($filePath), $flags);
	if (!$fileID) trigger_error("Unable to open \"$filePath\" for writing: ", E_USER_ERROR);
	if ($fileID) {
	 @fputs($fileID, $data);
	 @fclose($fileID);
	}
	// TO MIRROR BEHAVIOR IN file_put_contents() IN PHP 5+
	if (@is_file(actual_path($filePath)) && @filesize(actual_path($filePath)) > 0) return @filesize(actual_path($filePath)); else return false;
 }
}

    From the manual: This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.

    So why not do that?

      because my file_put_contents() has fopen(), fwrite() (or in this case, fputs()) and fclose(), so I am doing that.

        So are you saying that the function triggers an "Unable to open..." error? What happens if you don't suppress the error message from fopen?

        ppowell wrote:

        PHP 5+ is absolutely, positively, unequivocably never going to happen at this environment..

        Which is a shame, since PHP4 is no longer being supported.

          Weedpacket wrote:

          So are you saying that the function triggers an "Unable to open..." error? What happens if you don't suppress the error message from fopen?

          You see the "unable to open" warning, though action continues anyway

          Weedpacket wrote:

          Which is a shame, since PHP4 is no longer being supported.

          Service provider has no plans to upgrade at this time. Basically, you're on your own with PHP 4 and learn to live with it or move on. Oh, the temptation..

            ppowell wrote:

            You see the "unable to open" warning, though action continues anyway

            Thing is, [man]fopen[/man] triggers a warning message of its own if it fails to create a file.
            Ditto for [man]fputs[/man].

              Turns out the problem was totally unrelated.. directory permissions.. puh! Thanks

                Write a Reply...