PHP fopen() Functie
❮ Referentie voor PHP-bestandssysteemVoorbeeld
Open bestand, lees regels - totdat EOF is bereikt:
<?php
$file = fopen("test.txt", "r");
//Output lines until EOF is reached
while(! feof($file)) {
$line = fgets($file);
echo $line. "<br>";
}
fclose($file);
?>
Definitie en gebruik
De functie fopen() opent een bestand of URL.
Opmerking: wanneer u naar een tekstbestand schrijft, moet u ervoor zorgen dat u het juiste regeleinde gebruikt! Unix-systemen gebruiken \n, Windows-systemen gebruiken \r\n en Macintosh-systemen gebruiken \r als regeleinde. Windows biedt een vertaalvlag ('t') die \n naar \r\n zal vertalen als u met het bestand werkt. U kunt ook 'b' gebruiken om de binaire modus te forceren. Om deze vlaggen te gebruiken, specificeert u 'b' of 't' als het laatste teken van de modusparameter.
Syntaxis
fopen(filename, mode, include_path, context)
Parameterwaarden
Parameter | Description |
---|---|
filename | Required. Specifies the file or URL to open |
mode | Required. Specifies the type of access you require to the file/stream. Possible values:
|
include_path | Optional. Set this parameter to '1' if you want to search for the file in the include_path (in php.ini) as well |
context | Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream |
Technische details
Winstwaarde: | Een file pointer resource bij succes, FALSE en een fout bij mislukking. U kunt de fout verbergen door een "@" voor de functienaam toe te voegen. |
---|---|
PHP-versie: | 4.3+ |
PHP-wijzigingslogboek: | PHP 7.1: "e" optie toegevoegd PHP 5.2: "c" en "c+" opties toegevoegd |
❮ Referentie voor PHP-bestandssysteem