En son web geliştirme öğreticiler
 

PHP 5 Dosya Aç / Okuma / Kapat


Bu bölümde açmak, okumak ve sunucudaki bir dosyayı kapatmak için nasıl öğretecek.


PHP Dosya Aç - fopen()

Dosyaları açmak için daha iyi bir yöntem ile fopen() fonksiyonu. Bu fonksiyon size daha fazla seçenek sunar readfile() fonksiyonu.

Biz metin dosyasını kullanacaktır "webdictionary.txt" derslerde,:

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

Fopen ilk parametresi () dosyanın adı açılacak ve ikinci parametre dosya açılmalıdır hangi modda belirtir içerir. Fopen () işlevi, belirtilen dosyayı açmak mümkün olup olmadığını Aşağıdaki örnek, bir mesaj da oluşturur:

Örnek

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
»Run örnek

İpucu: fread () ve fclose () işlevleri aşağıda açıklanacaktır.

dosya aşağıdaki modlardan birinde açılabilir:

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 Oku Dosyası - fread ()

fread () fonksiyonu açık dosyadan okur.

freadla ilk parametresi () okuma dosyasının adını içerir ve ikinci parametre okumak için bayt sayısını belirtir.

Aşağıdaki PHP kodu sonuna "webdictionary.txt" dosyasını okur:

fread($myfile,filesize("webdictionary.txt"));

PHP Dosyayı Kapat - fclose()

fclose() fonksiyonu açık olan dosyayı kapatmak için kullanılır.

Dolayısıyla, onlara bitirdikten sonra tüm dosyaları kapatmak için iyi bir programlama uygulamadır. Eğer sunucu kaynaklarını alarak üzerinde koşturup bir açık dosyayı istemiyoruz!

fclose() dosyası (veya dosya adını tutan bir değişkenin) biz kapatmak istiyor adını gerektirir:

<?php
$myfile = fopen("webdictionary.txt", "r") ;
// some code to be executed....
fclose($myfile) ;
?>

PHP Oku Tek Hattı - fgets()

fgets() işlevi, bir dosyadan tek satır okumak için kullanılır.

Aşağıdaki örnekte ilk satırı verir "webdictionary.txt" dosyası:

Örnek

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
»Run örnek

Note: kullanıldıktan sonra fgets() işlevi, dosya işaretçisini sonraki satıra taşındı.


PHP Kontrol-Sonu File - feof()

feof() fonksiyonu kontrol eder, eğer "end-of-file" (EOF) ulaşıldı.

feof() işlevi bilinmemektedir uzunlukta veri döngü için yararlıdır.

Aşağıdaki örnek okur "webdictionary.txt" dosya-sonu ulaşılana kadar, dosyayı satır satır:

Örnek

<?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);
?>
»Run örnek

PHP Oku Tek Karakter - fgetc()

fgetc() işlevi, bir dosyadan tek bir karakter okumak için kullanılır.

Aşağıdaki örnek okur "webdictionary.txt" dosya-sonu ulaşılana kadar, karakteri ile dosya karakteri:

Örnek

<?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);
?>
»Run örnek

Note: çağrısından sonra fgetc() fonksiyonu, dosya işaretçisi sonraki karaktere taşır.


PHP Dosya Sistemi Referans tamamlayın

Dosya sistemi fonksiyonları tam referans için lütfen tüm gidin PHP Dosya Sistemi Referans .