Ever want to use [man]file_put_contents/man in PHP 4? It's a very helpful function. Well, I've made this to create that function in PHP4. The only difference is that this function doesn't support resource context.

If you find any changes/improvements please let me know. Thanks

// Check to see if functin exists
if (!function_exists('file_put_contents')) {

// Define constants used by function, if not defined
if (!defined('FILE_USE_INCLUDE_PATH')) define('FILE_USE_INCLUDE_PATH', 1);
if (!defined('FILE_APPEND'))           define('FILE_APPEND', 8);

// Define function and arguments
function file_put_contents($file, &$data, $flags=0)
{
    // Varify arguments are correct types
    if (!is_string($file)) return(false);
    if (!is_string($data) && !is_array($data)) return(false);
    if (!is_int($flags)) return(false);

    // Set the include path and mode for fopen
    $include = false;
    $mode    = 'wb';

    // If data in array type..
    if (is_array($data)) {
        // Make sure it's not multi-dimensional
        reset($data);
        while (list(, $value) = each($data)) {
            if (is_array($value)) return(false);
        }
        unset($value);
        reset($data);
        // Join the contents
        $data = implode('', $data);
    }

    // Check for flags..
    // If include path flag givin, set include path
    if ($flags&FILE_USE_INCLUDE_PATH) $include = true;
    // If append flag givin, set append mode
    if ($flags&FILE_APPEND) $mode = 'ab';

    // Open the file with givin options
    if (!$handle = @fopen($file, $mode, $include)) return(false);
    // Write data to file
    if (($bytes = fwrite($handle, $data)) === false) return(false);
    // Close file
    fclose($handle);

    // Return number of bytes written
    return($bytes);
}
}
    6 days later

    Just for the heck of it I made [man]file_get_contents/man for those who have PHP older than version 4.3.0. Of corse it doesn't support any of the PHP 5 features, resource context and offset.

    // Check to see if functin exists
    if (!function_exists('file_get_contents')) {
    
    // Define function and arguments
    function file_get_contents($file, $include=false)
    {
        // Varify arguments are correct types
        if (!is_string($file))  return(false);
        if (!is_bool($include)) return(false);
    
        // Open the file with givin options
        if (!$handle = @fopen($file, 'rb', $include)) return(false);
        // Read data from file
        $contents = fread($handle, filesize($file));
        // Close file
        fclose($handle);
    
        // Return contents of file
        return($contents);
    }
    }
    
      18 days later

      Thanks to laserlight I have fixed the file_put_contents() function above to properly detect flags. I also changed the $data argument to pass as refrence, for preformance. AND, I changed the foreach loop to while so now I THINK it should support PHP 3.0.7+ (not tested)!! If someone could test that it would be great. Thanks

        7 months later

        I found this useful to keep code valid for several servers in different states up upgrading. See # CHANGE # below:

        if (!is_string("$data") && !is_array($data)) return(false); # CHANGE #

        I found quoting the $data variable helpful to recognize a simple number as a string.

        Randy

        rpanning wrote:
        // Check to see if functin exists
        if (!function_exists('file_put_contents')) {
        
        // Define constants used by function, if not defined
        if (!defined('FILE_USE_INCLUDE_PATH')) define('FILE_USE_INCLUDE_PATH', 1);
        if (!defined('FILE_APPEND'))           define('FILE_APPEND', 8);
        
        // Define function and arguments
        function file_put_contents($file, &$data, $flags=0)
        {
            // Varify arguments are correct types
            if (!is_string($file)) return(false);
            if (!is_string("$data") && !is_array($data)) return(false); # CHANGE #
            if (!is_int($flags)) return(false);
        
            // Set the include path and mode for fopen
            $include = false;
            $mode    = 'wb';
        
            // If data in array type..
            if (is_array($data)) {
                // Make sure it's not multi-dimensional
                reset($data);
                while (list(, $value) = each($data)) {
                    if (is_array($value)) return(false);
                }
                unset($value);
                reset($data);
                // Join the contents
                $data = implode('', $data);
            }
        
            // Check for flags..
            // If include path flag givin, set include path
            if ($flags&FILE_USE_INCLUDE_PATH) $include = true;
            // If append flag givin, set append mode
            if ($flags&FILE_APPEND) $mode = 'ab';
        
            // Open the file with givin options
            if (!$handle = @fopen($file, $mode, $include)) return(false);
            // Write data to file
            if (($bytes = fwrite($handle, $data)) === false) return(false);
            // Close file
            fclose($handle);
        
            // Return number of bytes written
            return($bytes);
        }
        }
        
          7 months later

          You are returning here without closing your file resource ...

              // Write data to file 
              if (($bytes = fwrite($handle, $data)) === false) return(false);

            Wow, this thread has been resurrected from January last year 🙂

            I found quoting the $data variable helpful to recognize a simple number as a string.

            If you do that, is_string() must always return true since you are casting $data to string. With that in mind, I think that one can actually just skip the validation and typecast immediately.

            You are returning here without closing your file resource ...

            That probably doesnt matter for the most part since the file would be closed anyway when the script ends. Still, one does not even need to check the return value of fwrite() since it returns false if the write fails.

            Adding support for the exclusive lock option, I would contribute:

            if (!function_exists('file_put_contents')) {
            	// Define flags related to file_put_contents(), if necessary
            	if (!defined('FILE_USE_INCLUDE_PATH')) {
            		define('FILE_USE_INCLUDE_PATH', 1);
            	}
            	if (!defined('FILE_APPEND')) {
            		define('FILE_APPEND', 8);
            	}
            
            function file_put_contents($filename, $data, $flags = 0) {
            	// Handle single dimensional array data
            	if (is_array($data)) {
            		// Join the array elements
            		$data = implode('', $data);
            	}
            
            	// Flags should be an integral value
            	$flags = (int)$flags;
            	// Set the mode for fopen(), defaulting to 'wb'
            	$mode = ($flags & FILE_APPEND) ? 'ab' : 'wb';
            	$use_include_path = (bool)($flags & FILE_USE_INCLUDE_PATH);
            
            	// Open file with filename as a string
            	if ($fp = fopen("$filename", $mode, $use_include_path)) {
            		// Acquire exclusive lock if requested
            		if ($flags & LOCK_EX) {
            			if (!flock($fp, LOCK_EX)) {
            				fclose($fp);
            				return false;
            			}
            		}
            
            		// Write the data as a string
            		$bytes = fwrite($fp, "$data");
            
            		// Release exclusive lock if it was acquired
            		if ($flags & LOCK_EX) {
            			flock($fp, LOCK_UN);
            		}
            
            		fclose($fp);
            		return $bytes; // number of bytes written
            	} else {
            		return false;
            	}
            }
            }
              Write a Reply...