Write to File

The data file can be saved with fputs (int pointer, string string, int length). As with most of the functions that operate on files, it is necessary to specify the file pointer to where we want to operate.

You can save either the entire contents of the variable given as the second parameter, or only to a certain length, which should be reported as the third optional parameter (of course without taking this parameter is written all variable specified in the second parameter). Recording is done in place, which indicates an internal rate of the file, overwriting the data if this indicator is not the end of the file. There is no possibility of direct write data at the beginning or in the middle of the file. The only possibility is to load the file to a temporary variable, improving the data and rewriting the file.



//Load the old data

//Open the file for reading
$fp = fopen („file.txt”, „r”);

//Read the data
$olddata = fread ($fp, filesize („file.txt”));

//Close the file
fclose ($fp)

//Create a new data

$newData= „This is what you want it to be at the beginning of \ n”;
$newData. = $oldData;

// Store new data

// Open the file for writing
$fp = fopen („file.txt”, „w”);

// Save data
fputs ($fp, $newData);

// Close the file
fclose ($fp);
Writing at the end of a file is easy – just open the file in the „a” and immediately you dodować data to a file.


Close Menu