Files
Spesso si devono salvare o leggere una risorsa per lo sviluppo web un modo semplice per farlo è quello di leggere e scrivere da file.
Leggere un file di
Tratteremo il modo più semplice per leggere un file utilizzando la funzione
file_get_contents.
Simply give it the relative path of the file to read / / Basta dare il percorso relativo del file da leggere
contents = file_get_contents ( ' text . txt ' ) ; $ Contenuto = file_get_contents ('testo txt.');
$ contents ; echo $ contenuto;
Il file legge il file text.txt e restituisce in una stringa. Questo è il modo più semplice per leggere un file si può anche limitare la quantità di leggere in modo
contents = file_get_contents ( ' text . txt ' , false , null , 0 , 1024 ) ; $ Contenuto = file_get_contents ('testo txt.', False, null, 0, 1024);
Si possono leggere i primi 1024 byte o kilobyte prima. È inoltre possibile utilizzare le
file_get_contents funzione per leggere pagine web in modo da:
Simply give it the url of the web page to read / / Basta dare l'url della pagina web da leggere
contents = file_get_contents ( ' http : // www . google . com ' ) ; $ Contenuto = file_get_contents ('.. Http: / / www google com');
Leggerà Googles pagina principale web!
Scrivere in un file
Il modo più semplice per scrivere in un file è quello di utilizzare la funzione
file_put_contents.
Simply give it the file to write to and the data / / Basta dare il file da scrivere e dei dati
data = ' hi there ' ; $ Data = 'Ciao';
' text . txt ' , $ data ) ; file_put_contents ('testo txt.', $ dati);
Questo restituisce false se ci fosse un errore o un numero di byte che è stato scritto se fosse successo in modo che possiamo migliorare ulteriormente questo codice con una logica del tipo:
Simply give it the file to write to and the data / / Basta dare il file da scrivere e dei dati
data = ' hi there ' ; $ Data = 'Ciao';
file_put_contents ( ' text . txt ' , $ data ) ) if (! file_put_contents ('testo. txt', $ data))
{
" There was an error writing your data to the text . txt file " ; echo "Si è verificato un errore durante la scrittura dei dati sul file di testo txt.";
}
Questa funzione effettivamente creare un file non esistente di default e così come sovrascrivere i file esistenti. Quindi potrebbe essere una buona idea lasciare solo il file aggiungere impostando il flag append file.
Simply give it the file to write to and the data / / Basta dare il file da scrivere e dei dati
data = ' hi there ' ; $ Data = 'Ciao';
file_put_contents ( ' text . txt ' , $ data , FILE_APPEND ) ) if (! file_put_contents ('testo. txt', $ data, FILE_APPEND))
{
" There was an error writing your data to the text . txt file " ; echo "Si è verificato un errore durante la scrittura dei dati sul file di testo txt.";
}
Buone pratiche
Di solito quando si tratta di file che è bene per vedere se il file esiste prima di fare qualcosa con esso, questo rende il codice più affidabile. Per eseguire questa funzione delle
file_exists può essere utilizzata.
Simply give it the file name to check that exists / / Basta dare il nome del file per verificare che esiste
file_exists ( ' text . txt ' ) ) if (! file_exists ('testo. txt'))
{
" Your file doesn ' t exist ! " ; echo "Il file doesn 't esiste!";
ritorno;
}
/ *
Rest of the file accessing code . * Resto del codice di accesso al file.
* /
Nessun commento è stato fornito.
Scritto da Dominic Skinner
Ultimo aggiornamento: 2011/10/25 16:00:38