PHP file_put_contents() Functie
❮ Referentie voor PHP-bestandssysteemVoorbeeld
Schrijf gegevens naar een bestand:
<?php
echo file_put_contents("test.txt","Hello World. Testing!");
?>
De uitvoer van de bovenstaande code zal zijn:
21
Definitie en gebruik
De file_put_contents() schrijft gegevens naar een bestand.
Deze functie volgt deze regels bij het openen van een bestand:
- Als FILE_USE_INCLUDE_PATH is ingesteld, controleer dan het include-pad voor een kopie van de bestandsnaam
- Maak het bestand als het niet bestaat
- Open het bestand
- Vergrendel het bestand als LOCK_EX is ingesteld
- Als FILE_APPEND is ingesteld, gaat u naar het einde van het bestand. Wis anders de bestandsinhoud
- Schrijf de gegevens in het bestand
- Sluit het bestand en ontgrendel eventuele vergrendelingen
Opmerking: gebruik FILE_APPEND om te voorkomen dat de bestaande inhoud van het bestand wordt verwijderd.
Syntaxis
file_put_contents(filename, data, mode, context)
Parameterwaarden
Parameter | Description |
---|---|
filename | Required. Specifies the path to the file to write to. If the file does not exist, this function will create one |
data | Required. The data to write to the file. Can be a string, array, or a data stream |
mode | Optional. Specifies how to open/write to the file. Possible values:
|
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: | Het aantal bytes dat in het bestand is geschreven bij succes, ONWAAR bij mislukking |
---|---|
PHP-versie: | 5.0+ |
Binaire kluis: | Ja |
PHP-wijzigingslogboek: | PHP 5.1 - Ondersteuning toegevoegd voor LOCK_EX en de mogelijkheid om een streambron door te geven aan de dataparameter |
❮ Referentie voor PHP-bestandssysteem