Samouczek PHP

Strona główna PHP Wprowadzenie do PHP Instalacja PHP Składnia PHP Komentarze PHP Zmienne PHP PHP Echo / Drukuj Typy danych PHP Ciągi PHP Liczby PHP Matematyka w PHP Stałe PHP Operatory PHP PHP Jeśli...Inne...Elseif Przełącznik PHP Pętle PHP Funkcje PHP Tablice PHP PHP Superglobals PHP RegEx

Formularze PHP

Obsługa formularzy PHP Walidacja formularzy PHP Wymagany formularz PHP Adres URL/e-mail formularza PHP Formularz PHP kompletny

Zaawansowany PHP

Data i godzina w PHP Uwzględnij PHP Obsługa plików PHP Otwórz/odczytaj plik PHP Tworzenie/zapisywanie plików PHP Przesyłanie plików PHP Pliki cookie PHP Sesje PHP Filtry PHP Zaawansowane filtry PHP Funkcje wywołania zwrotnego PHP PHP JSON Wyjątki PHP

PHP OOP

PHP Co to jest OOP Klasy/obiekty PHP Konstruktor PHP Destruktor PHP Modyfikatory dostępu PHP Dziedziczenie PHP Stałe PHP Klasy abstrakcyjne PHP Interfejsy PHP Cechy PHP Metody statyczne PHP Właściwości statyczne PHP Przestrzenie nazw PHP Iterowalne PHP

Baza danych MySQL

Baza danych MySQL Połączenie MySQL Tworzenie bazy danych MySQL Utwórz tabelę MySQL Wstaw dane MySQL Pobierz ostatni identyfikator MySQL Wstaw wiele MySQL Przygotowano MySQL Wybierz dane MySQL MySQL Gdzie Zamów MySQL według Usuń dane MySQL Dane aktualizacji MySQL Dane limitu MySQL

PHP XML

Parsery PHP XML Parser PHP SimpleXML PHP SimpleXML — Get Rozszerzenie PHP XML PHP XML DOM

PHP - AJAX

Wprowadzenie do AJAX AJAX PHP Baza danych AJAX XML AJAX Wyszukiwanie na żywo AJAX Ankieta AJAX

Przykłady PHP

Przykłady PHP Kompilator PHP Quiz PHP Ćwiczenia PHP Certyfikat PHP

Odniesienie do PHP

Przegląd PHP Tablica PHP Kalendarz PHP Data PHP Katalog PHP Błąd PHP Wyjątek PHP System plików PHP Filtr PHP PHP FTP PHP JSON Słowa kluczowe PHP Biblioteka PHP Poczta PHP Matematyka w PHP Różne PHP PHP MySQLi Sieć PHP Kontrola wyjścia PHP PHP RegEx PHP SimpleXML Strumień PHP ciąg PHP Obsługa zmiennych PHP Parser PHP XML Kod pocztowy PHP Strefy czasowe PHP

Funkcja fopen() PHP

❮ Dokumentacja systemu plików PHP

Przykład

Otwórz plik, czytaj wiersze - aż do osiągnięcia EOF:

<?php
$file = fopen("test.txt", "r");

//Output lines until EOF is reached
while(! feof($file)) {
  $line = fgets($file);
  echo $line. "<br>";
}

fclose($file);
?>

Definicja i użycie

Funkcja fopen() otwiera plik lub adres URL.

Uwaga: Pisząc do pliku tekstowego, upewnij się, że używasz prawidłowego znaku końca linii! Systemy Unix używają \n, Windows używają \r\n, a Macintosh używają \r jako znaku końca linii. Windows oferuje flagę tłumaczenia ('t'), która przetłumaczy \n na \r\n podczas pracy z plikiem. Możesz także użyć 'b', aby wymusić tryb binarny. Aby użyć tych flag, podaj „b” lub „t” jako ostatni znak parametru trybu.

Składnia

fopen(filename, mode, include_path, context)

Wartości parametrów

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:

  • "r" - Read only. Starts at the beginning of the file
  • "r+" - Read/Write. Starts at the beginning of the file
  • "w" - Write only. Opens and truncates the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
  • "w+" - Read/Write. Opens and truncates the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
  • "a" - Write only. Opens and writes to the end of the file or creates a new file if it doesn't exist
  • "a+" - Read/Write. Preserves file content by writing to the end of the file
  • "x" - Write only. Creates a new file. Returns FALSE and an error if file already exists
  • "x+" - Read/Write. Creates a new file. Returns FALSE and an error if file already exists
  • "c" - Write only. Opens the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
  • "c+" - Read/Write. Opens the file; or creates a new file if it doesn't exist. Place file pointer at the beginning of the file
  • "e" - Only available in PHP compiled on POSIX.1-2008 conform systems.
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


Szczegóły techniczne

Wartość zwrotu: Zasób wskaźnika pliku w przypadku powodzenia, FALSE i błąd w przypadku niepowodzenia. Możesz ukryć błąd, dodając „@” przed nazwą funkcji.
Wersja PHP: 4.3+
Dziennik zmian PHP: PHP 7.1: Dodano opcję "e"
PHP 5.2: Dodano opcje "c" i "c+"

❮ Dokumentacja systemu plików PHP