Your Developer Exam showed me just how much there is to PHP. "El examen de Desarrollador me mostró lo mucho que hay para PHP."
Ross

Archivos

A menudo se tiene que guardar o leer un recurso en el desarrollo web de una manera fácil de hacer esto es a leer y escribir a partir de archivos.

Lectura de un archivo

Vamos a cubrir la forma más sencilla de leer un archivo mediante la función file_get_contents.
Simply give it the relative path of the file to read / / Simplemente darle la ruta relativa del archivo para leer
contents = file_get_contents ( ' text . txt ' ) ; $ Contenido = file_get_contents ('texto txt.');
$ contents ; echo $ contenido;
El archivo se lee el archivo y lo envía text.txt en una cadena. Esta es la forma más sencilla de leer un archivo también puede limitar la cantidad que usted lea lo
contents = file_get_contents ( ' text . txt ' , false , null , 0 , 1024 ) ; $ Contenido = file_get_contents ('texto txt.', False, null, 0, 1024);
Se leen los primeros 1024 bytes o kilobyte en primer lugar. También puede utilizar los file_get_contents funcionar para leer las páginas web para:
Simply give it the url of the web page to read / / Simplemente dar la url de la página web para leer
contents = file_get_contents ( ' http : // www . google . com ' ) ; $ Contenido = file_get_contents (".. Http: / / www google com ');
Leerá Googles página web principal!

Escribir en un archivo

La forma más sencilla de escribir en un archivo es usar la función file_put_contents.
Simply give it the file to write to and the data / / Simplemente darle el archivo para escritura y los datos
data = ' hi there ' ; $ Datos = 'hola';
' text . txt ' , $ data ) ; file_put_contents ('texto txt.', $ datos);
Esto devuelve false si hubo un error o un número de bytes que se han escrito, si se realiza con éxito por lo que puede mejorar aún más este código con cierta lógica, como:
Simply give it the file to write to and the data / / Simplemente darle el archivo para escritura y los datos
data = ' hi there ' ; $ Datos = 'hola';
file_put_contents ( ' text . txt ' , $ data ) ) if (! file_put_contents ('texto. txt', $ datos))
{
" There was an error writing your data to the text . txt file " ; echo "Se ha producido un error al escribir los datos en el archivo de texto txt.";
}
Esta función en realidad creará un archivo que no existe por defecto y, así como sobrescribir archivos existentes. Por lo tanto, podría ser una buena idea dejar que sólo el archivo agregar mediante el establecimiento de la bandera de adjuntar el archivo.
Simply give it the file to write to and the data / / Simplemente darle el archivo para escritura y los datos
data = ' hi there ' ; $ Datos = 'hola';
file_put_contents ( ' text . txt ' , $ data , FILE_APPEND ) ) if (! file_put_contents ('texto. txt', $ datos, FILE_APPEND))
{
" There was an error writing your data to the text . txt file " ; echo "Se ha producido un error al escribir los datos en el archivo de texto txt.";
}

Las buenas prácticas

Por lo general, cuando se trata de archivos es bueno para ver si existe el archivo antes de hacer algo con él, esto hace que el código sea más fiable. Para hacer esta función los file_exists puede ser utilizado.
Simply give it the file name to check that exists / / Simplemente darle el nombre del archivo para comprobar que existe
file_exists ( ' text . txt ' ) ) if (! file_exists ('texto. txt "))
{
" Your file doesn ' t exist ! " ; echo "El archivo doesn 't existe!";
regresar;
}
/ *
Rest of the file accessing code . * El resto del código de acceso al archivo.
* /

No hay comentarios han sido proporcionados.
la seguridad de la imagen
Escrito por Domingo Skinner
Última actualización: 25/10/2011 16:00:38