File PHP Apri/Leggi/Chiudi
In questo capitolo ti insegneremo come aprire, leggere e chiudere un file sul server.
File aperto PHP - fopen()
Un metodo migliore per aprire i file è con la fopen()
funzione. Questa funzione offre più opzioni rispetto alla readfile()
funzione.
Utilizzeremo il file di testo, "webdictionary.txt", durante le lezioni:
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
Il primo parametro fopen()
contiene il nome del file da aprire e il secondo parametro specifica in quale modalità deve essere aperto il file. L'esempio seguente genera anche un messaggio se la funzione fopen() non è in grado di aprire il file specificato:
Esempio
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
Suggerimento: le funzioni fread()
e fclose()
verranno spiegate di seguito.
Il file può essere aperto in una delle seguenti modalità:
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 |
File di lettura PHP - fread()
La fread()
funzione legge da un file aperto.
Il primo parametro di fread()
contiene il nome del file da cui leggere e il secondo parametro specifica il numero massimo di byte da leggere.
Il seguente codice PHP legge il file "webdictionary.txt" fino alla fine:
fread($myfile,filesize("webdictionary.txt"));
File di chiusura PHP - fclose()
La fclose()
funzione viene utilizzata per chiudere un file aperto.
È una buona pratica di programmazione chiudere tutti i file dopo averli terminati. Non vuoi che un file aperto in giro sul tuo server occupi risorse!
Richiede il fclose()
nome del file (o una variabile che contiene il nome del file) che vogliamo chiudere:
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
PHP Leggi riga singola - fgets()
La fgets()
funzione viene utilizzata per leggere una singola riga da un file.
L'esempio seguente restituisce la prima riga del file "webdictionary.txt":
Esempio
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
Nota: dopo una chiamata alla fgets()
funzione, il puntatore del file si è spostato sulla riga successiva.
PHP Verifica fine file - feof()
La feof()
funzione verifica se è stata raggiunta la "fine del file" (EOF).
La feof()
funzione è utile per scorrere dati di lunghezza sconosciuta.
L'esempio seguente legge il file "webdictionary.txt" riga per riga, fino a raggiungere la fine del file:
Esempio
<?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 Leggi un singolo carattere - fgetc()
La fgetc()
funzione viene utilizzata per leggere un singolo carattere da un file.
L'esempio seguente legge il file "webdictionary.txt" carattere per carattere, fino al raggiungimento della fine del file:
Esempio
<?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);
?>
Nota: dopo una chiamata alla fgetc()
funzione, il puntatore del file si sposta sul carattere successivo.
Riferimento completo al file system PHP
Per un riferimento completo delle funzioni del filesystem, vai al nostro riferimento completo al filesystem PHP .