2011-04-03 17:26:38 0 Comments PHP Boy.Lee

save a file in PHP

{Function}

resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]] )
int fwrite ( resource handle, string string [, int length] )
bool fclose ( resource handle )

{Code Example}

	public function writeFile($fPath = './DBox/new.xml', $str='')
	{
    preg_match('|/.*/|', $fPath, $out);
    if (@is_writable($out[0]))
        if ($fp = fopen($fPath, 'w')){
            fwrite($fp, $str);
            fclose($fp);
        }
	}

 

{EN} 

mode

Description

'r'

Open for reading only; place the file pointer at the beginning of the file.

'r+'

Open for reading and writing; place the file pointer at the beginning of the file.

'w'

Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

'w+'

Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

'a'

Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

'a+'

Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

'x'

Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.

'x+'

Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.