PHP:Fitxers: diferència entre les revisions
Salta a la navegació
Salta a la cerca
(→Llegir) |
(→Llegir) |
||
Línia 1: | Línia 1: | ||
==Llegir== | ==Llegir== | ||
<pre> | <pre> | ||
$nomfitxer = "fitxer.txt"; | |||
if (is_file($nomfitxer)) | |||
{ | |||
$fp = fopen($nomfitxer, "r"); | $fp = fopen($nomfitxer, "r"); | ||
if ($fp) { | if ($fp) { | ||
Línia 9: | Línia 9: | ||
{usleep(rand(1, 300));} | {usleep(rand(1, 300));} | ||
$content = ''; | $content = ''; | ||
while (!feof($ | while (!feof($fp)) { | ||
$content .= fread($ | $content .= fread($fp, 8192); | ||
} | } | ||
flock($fp, LOCK_UN); | flock($fp, LOCK_UN); | ||
fclose($fp); | fclose($fp); | ||
} | } | ||
} | } | ||
</pre> | |||
'''fgets''' enlloc del '''fread''' serveix per llegir linia a linia. | |||
===Llegir i partir en array per linies i camps=== | |||
<pre> | |||
if (is_file($nomfitxer)) | |||
{ | |||
$fp = fopen($nomfitxer, "r"); | |||
if ($fp) { | |||
while (flock($fp, LOCK_EX) == false) // do an exclusive lock | |||
{usleep(rand(1, 300));} | |||
$i = 0; | |||
while (!feof($fp)) { | |||
$content[$i]= split(';',fgets($fp, 8192)); | |||
$i++; | |||
} | |||
flock($fp, LOCK_UN); | |||
fclose($fp); | |||
} | |||
} | |||
echo " content 0 0 ="; | |||
echo $content[0][0]; | |||
echo " content 0 2 ="; | |||
echo $content[0][2]; | |||
</pre> | </pre> | ||
==Escriure== | ==Escriure== |
Revisió del 14:00, 18 des 2009
Llegir
$nomfitxer = "fitxer.txt"; if (is_file($nomfitxer)) { $fp = fopen($nomfitxer, "r"); if ($fp) { while (flock($fp, LOCK_EX) == false) // do an exclusive lock {usleep(rand(1, 300));} $content = ''; while (!feof($fp)) { $content .= fread($fp, 8192); } flock($fp, LOCK_UN); fclose($fp); } }
fgets enlloc del fread serveix per llegir linia a linia.
Llegir i partir en array per linies i camps
if (is_file($nomfitxer)) { $fp = fopen($nomfitxer, "r"); if ($fp) { while (flock($fp, LOCK_EX) == false) // do an exclusive lock {usleep(rand(1, 300));} $i = 0; while (!feof($fp)) { $content[$i]= split(';',fgets($fp, 8192)); $i++; } flock($fp, LOCK_UN); fclose($fp); } } echo " content 0 0 ="; echo $content[0][0]; echo " content 0 2 ="; echo $content[0][2];
Escriure
$nomfitxer = "fitxer.txt"; $fp = fopen($nomfitxer , "w+"); if ($fp) { while (flock($fp, LOCK_EX) == false) // do an exclusive lock {usleep(rand(1, 300));} fwrite($fp, $content); flock($fp, LOCK_UN); fclose($fp); }
"a+" enlloc de "w+" serveix per afegir al final del fitxer.
Afegir a un registre
$fp = fopen("fitxer.txt", "a+"); fwrite($fp, date("Ymd G:i:s") . "\t" . $_SERVER{'DOCUMENT_ROOT'} . "\t" . $_SERVER{'REQUEST_URI'} . "\t" . $_SERVER{'REDIRECT_URL'} . "\n"); fclose($fp);
Afegir a un registre informació de l'usuari
$elim = array("\n", "\r", "\t", '$', "\\'", "'", "\\"); $info_usuari= str_replace($elim,' ',$HTTP_ACCEPT_LANGUAGE) . "\t" . str_replace($elim,' ',$HTTP_USER_AGENT) . "\t" . str_replace($elim,' ',$HTTP_ACCEPT) . "\t" . str_replace($elim,' ',$HTTP_ACCEPT_ENCODING) . "\t" . str_replace($elim,' ',$HTTP_ACCEPT_CHARSET); if ($_SERVER{'HTTP_X_FORWARDED_FOR'} != "" && $_SERVER{'HTTP_X_FORWARDED_FOR'} != "127.0.0.1") $ipusuari= $_SERVER{'HTTP_X_FORWARDED_FOR'}; else $ipusuari= $_SERVER{'REMOTE_ADDR'}; if ($ipusuari != "") $hostusuari = gethostbyaddr($ipusuari); $fp = fopen("fitxer.txt", "a+"); fwrite($fp, date("Ymd G:i:s") . "\t" . $_SERVER{'REMOTE_ADDR'} . "\t" . $_SERVER{'HTTP_X_FORWARDED_FOR'} . "\t" . $hostusuari . "\t" . $_SERVER{'DOCUMENT_ROOT'} . "\t" . $_SERVER{'REQUEST_URI'} . "\t" . $_SERVER{'REDIRECT_URL'} . "\t" . $info_usuari . "\n"); fclose($fp);