Soubory a data
Níže je uveden pouze náhled materiálu. Kliknutím na tlačítko 'Stáhnout soubor' stáhnete kompletní formátovaný materiál ve formátu PDF.
'x+'
Create and open for reading and writing; otherwise it has the same behavior as 'x'.
'c'
Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither
truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The
file pointer is positioned on the beginning of the file. This may be useful if it's desired to get
an advisory lock (see flock()) before attempting to modify the file, as using 'w' could
truncate the file before the lock was obtained (if truncation is desired, ftruncate() can be
used after the lock is requested).
'c+'
Open the file for reading and writing; otherwise it has the same behavior as 'c'.
Čtení souboru, zavření souboru
string fread ( resource $handle , int $length )
<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>
Zápis do souboru
Zapisovat se dá buď text nebo binární data
– …případně jiné struktury
int fwrite ( resource $handle , string $string [, int $length ] )
<?php
$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
// the content of 'data.txt' is now 123 and not 23!
?>
Serializace nativní
Proces převedení dat do binární (textové) podoby
string serialize ( mixed $value )
mixed unserialize ( string $str [, array $options ] )
<?php $a= array( '1' => 'elem 1', '2'=> 'elem 2', '3'=>' elem 3');
print_r($a); echo ("<br></br>");
$b=serialize($a);
print_r($b); ?>
Array ( [1] => elem 1 [2] => elem 2 [3] => elem 3 )
a:3:{i:1;s:6:"elem 1";i:2;s:6:"elem 2";i:3;s:7:" elem 3";
Výstup
Serializace JSON
Častěji používané, rozumí tomu ostatní
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);