Changeset 330


Ignore:
Timestamp:
May 6, 2008 9:21:27 AM (16 years ago)
Author:
quinn
Message:

Added filePutContents to emulate PHP5's file_put_content() function.

Location:
trunk/lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Currency.inc.php

    r329 r330  
    111111    {
    112112        $app =& App::getInstance();
    113        
     113
    114114        $cache_file_path = sprintf('%s/%s-to-%s', $this->getParam('cache_dir'), $base, $target);
    115115        $cache_file_mtime = @filemtime($cache_file_path);
     
    129129                    return false;
    130130                }
    131             } else if ($this->getParam('cache_result') && !file_put_contents($cache_file_path, $value, LOCK_EX)) {
     131            } else if ($this->getParam('cache_result') && !filePutContents($cache_file_path, $value, LOCK_EX)) {
    132132                $app->logMsg(sprintf('Failed writing to target rate file: %s', $cache_file_path), LOG_ERR, __FILE__, __LINE__);
    133133                return false;
  • trunk/lib/Utilities.inc.php

    r320 r330  
    398398}
    399399
     400/*
     401* Writes content to the specified file. This function emulates the functionality of file_put_contents from PHP 5.
     402*
     403* @access   public
     404* @param    string  $filename   Path to file.
     405* @param    string  $content    Data to write into file.
     406* @return   bool                Success or failure.
     407* @author   Quinn Comendant <quinn@strangecode.com>
     408* @since    11 Apr 2006 22:48:30
     409*/
     410function filePutContents($filename, $content)
     411{
     412    $app =& App::getInstance();
     413
     414    // Open file for writing and truncate to zero length.
     415    if ($fp = fopen($filename, 'w')) {
     416        if (flock($fp, LOCK_EX)) {
     417            if (!fwrite($fp, $content, mb_strlen($content))) {
     418                $app->logMsg(sprintf('Failed writing to file: %s', $filename), LOG_ERR, __FILE__, __LINE__);
     419                fclose($fp);
     420                return false;
     421            }
     422            flock($fp, LOCK_UN);
     423        } else {
     424            $app->logMsg(sprintf('Could not lock file for writing: %s', $filename), LOG_ERR, __FILE__, __LINE__);
     425            fclose($fp);
     426            return false;
     427        }
     428        fclose($fp);
     429        // Success!
     430        $app->logMsg(sprintf('Wrote to file: %s', $filename), LOG_DEBUG, __FILE__, __LINE__);
     431        return true;
     432    } else {
     433        $app->logMsg(sprintf('Could not open file for writing: %s', $filename), LOG_ERR, __FILE__, __LINE__);
     434        return false;
     435    }
     436}
     437
     438
    400439/**
    401440 * If $var is net set or null, set it to $default. Otherwise leave it alone.
Note: See TracChangeset for help on using the changeset viewer.