PHP- bestand openen/lezen/sluiten
In dit hoofdstuk leren we u hoe u een bestand op de server opent, leest en sluit.
PHP Bestand openen - fopen()
Een betere methode om bestanden te openen is met de fopen()
functie. Deze functie geeft je meer opties dan de readfile()
functie.
We zullen tijdens de lessen het tekstbestand "webdictionary.txt" gebruiken:
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
De eerste parameter van fopen()
bevat de naam van het te openen bestand en de tweede parameter geeft aan in welke modus het bestand moet worden geopend. Het volgende voorbeeld genereert ook een bericht als de functie fopen() het opgegeven bestand niet kan openen:
Voorbeeld
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
Tip: De fread()
en de fclose()
functies worden hieronder uitgelegd.
Het bestand kan in een van de volgende modi worden geopend:
Modes | Description |
---|---|
r | Open a file for read only. File pointer starts at the beginning of the file |
w | Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a | Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x | Creates a new file for write only. Returns FALSE and an error if file already exists |
r+ | Open a file for read/write. File pointer starts at the beginning of the file |
w+ | Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a+ | Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x+ | Creates a new file for read/write. Returns FALSE and an error if file already exists |
PHP-leesbestand - fread()
De fread()
functie leest uit een geopend bestand.
De eerste parameter van fread()
bevat de naam van het bestand waaruit moet worden gelezen en de tweede parameter specificeert het maximum aantal te lezen bytes.
De volgende PHP-code leest het bestand "webdictionary.txt" tot het einde:
fread($myfile,filesize("webdictionary.txt"));
PHP Bestand sluiten - fclose()
De fclose()
functie wordt gebruikt om een geopend bestand te sluiten.
Het is een goede programmeerpraktijk om alle bestanden te sluiten nadat u ermee klaar bent. U wilt niet dat een open bestand op uw server rondloopt en resources in beslag neemt!
Het fclose()
vereist de naam van het bestand (of een variabele die de bestandsnaam bevat) die we willen sluiten:
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
PHP lezen enkele regel - fgets()
De fgets()
functie wordt gebruikt om een enkele regel uit een bestand te lezen.
Het onderstaande voorbeeld geeft de eerste regel van het bestand "webdictionary.txt" weer:
Voorbeeld
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
Opmerking: na een aanroep van de fgets()
functie is de bestandsaanwijzer naar de volgende regel verplaatst.
PHP-controle einde bestand - feof()
De feof()
functie controleert of de "end-of-file" (EOF) is bereikt.
De feof()
functie is handig voor het doorlopen van gegevens van onbekende lengte.
In het onderstaande voorbeeld wordt het bestand "webdictionary.txt" regel voor regel gelezen, totdat het einde van het bestand is bereikt:
Voorbeeld
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>
PHP lezen enkel teken - fgetc()
De fgetc()
functie wordt gebruikt om een enkel teken uit een bestand te lezen.
In het onderstaande voorbeeld wordt het bestand "webdictionary.txt" teken voor teken gelezen, totdat het einde van het bestand is bereikt:
Voorbeeld
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
Opmerking: na een aanroep van de fgetc()
functie gaat de bestandsaanwijzer naar het volgende teken.
Volledige PHP-bestandssysteemreferentie
Ga voor een volledige referentie van bestandssysteemfuncties naar onze volledige PHP- bestandssysteemreferentie .