Scripts - PHP - Werken met files (php)


Werken met files (php)

In PHP kunnen we de webserver met files laten werken: openen, informatie eruit lezen of erin schrijven.
PHP kan omgaan met tekstfiles en binaire files.
Hieronder enkele voorbeelden:

Schrijven in een tekstfile

$defile = fopen("oefenfile.txt","a+");
if($defile == FALSE){
echo "Ik kan de file niet openen voor toevoegen";
exit();
}

$nieuweregel="Een regel erbij";
fwrite($defile,"$nieuweregel\n"); // voeg een regel toe
fclose($defile);

Lezen uit een tekstfile

$defile = fopen("oefenfile.txt","r");
if($defile == FALSE){
echo "Ik kan de file niet openen om eruit te lezen";
exit();
}

$regelnummer=1;
while(!feof($defile)){
$volgenderegel = fgets($defile);
if($volgenderegel=="")break;

echo "Regel $regelnummer: $volgenderegel<br>";

$regelnummer++;
} // while
fclose($defile);

File openen

De tabel hieronder geeft de verschillende manieren om een file te openen.
Deze tabel kun je ook vinden op de PHP-site bij de beschrijving van fopen()

'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. This option is supported in PHP 4.3.2 and later, and only works for local files.
'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. This option is supported in PHP 4.3.2 and later, and only works for local files.


Zoek pagina:


Calender / Nieuws

« September 2017 »
Nr Zo Ma Di Wo Do Vr Za
35     12
363456789
371011121314 15 16
38 1718 1920212223
39 24252627282930


Website info

Laatste update

22 Aug.
21 Aug.
26 Mei
27 Jan.

Website

Totaal aantal: 217 pagina's.
Bijgewerkt: 22-08-2017.

Weersverwachting
Leiden
Weeronline.nl - Meer weer in Leidenweeronline.nl Altijd jouw weer