PHP:Fitxers: diferència entre les revisions

De WikiMar
Salta a la navegació Salta a la cerca
(Pàgina nova, amb el contingut: «==Llegir== if (is_file($filename) { $fp = fopen("fitxer.txt", "r"); if ($fp) { while (flock($fp, LOCK_EX) == false) // do an exclusive lock {usleep(rand...».)
 
 
(13 revisions intermèdies per 4 usuaris que no es mostren)
Línia 1: Línia 1:
==Llegir i posar en Array per linies==
Maxim fitxers de 10MB.
$lines = file("myfile.txt");
foreach($lines as $line) {
    echo($line);
}
echo $line[0];
o també:
$lines = file('http://www.example.com/');
==Llegir==
==Llegir==
if (is_file($filename)
<pre>
{
$nomfitxer = "fitxer.txt";
$fp = fopen("fitxer.txt", "r");
if (is_file($nomfitxer))
if ($fp) {
{
while (flock($fp, LOCK_EX) == false)  // do an exclusive lock
$fp = fopen($nomfitxer, "r");
{usleep(rand(1, 300));}
if ($fp) {
$contents='';
while (flock($fp, LOCK_EX) == false)  // do an exclusive lock
while (!feof($handle)) {
{usleep(rand(1, 300));}
$contents .= fread($handle, 8192);
$content = '';
}
while (!feof($fp)) {
flock($fp, LOCK_UN);
$content .= fread($fp, 8192);
fclose($fp);
}
}
flock($fp, LOCK_UN);
}
fclose($fp);
}
}
</pre>
'''fgets''' enlloc del '''fread''' serveix per llegir linia a linia.


fget enlloc del fread serveix per llegir linia a linia.
===Llegir i partir en array per linies i camps===
<syntaxhighlight lang="php">
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];
</syntaxhighlight>


==Escriure==
==Escriure==
<syntaxhighlight lang="php">
  $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);
  }
</syntaxhighlight>
'''"a+"''' enlloc de '''"w+"''' serveix per afegir al final del fitxer.
===Lock file===
<syntaxhighlight lang="php">
        $fp = fopen($COUNT_FILE, "w");          //open the file for write
        while (flock($fp, LOCK_EX) == false)  // do an exclusive lock
        {usleep(rand(1, 600));}
        fputs($fp , "$visits[0]");          //write the new value to the file
        flock($fp, LOCK_UN); // release the lock
        fclose($fp);            //close the file
</syntaxhighlight>


if (is_file($filename)
===Check Size and rename===
{
<syntaxhighlight lang="php">
$fp = fopen("fitxer.txt", "w+");
date_default_timezone_set('CET');
if ($fp) {
if (is_file($logaccess)){
while (flock($fp, LOCK_EX) == false) // do an exclusive lock
if (filesize($logaccess) > 500000)
{usleep(rand(1, 300));}
{
fwrite($fd, $content);
rename($logaccess, $logaccess . "." . date("ymdHis") . ".txt");
flock($fp, LOCK_UN);
fclose($fp);
}
}
  }
  }
</syntaxhighlight>
==Afegir a un registre==
        date_default_timezone_set('CET');
$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==
<syntaxhighlight lang="php">
date_default_timezone_set('CET');
$elim = array("\n", "\r", "\t", '$', "\\'", "'", "\\");
$info_usuari  = str_replace($elim,' ',$_SERVER['HTTP_ACCEPT_LANGUAGE']) . "\t";
$info_usuari .= str_replace($elim,' ',$_SERVER['HTTP_USER_AGENT']) . "\t";
$info_usuari .= str_replace($elim,' ',$_SERVER['HTTP_ACCEPT']) . "\t";
$info_usuari .= str_replace($elim,' ',$_SERVER['HTTP_ACCEPT_ENCODING']) . "\t";
$info_usuari .= str_replace($elim,' ',$_SERVER['HTTP_ACCEPT_CHARSET']) . "\t";
$info_usuari .= str_replace($elim,' ',$_SERVER['HTTP_REFERER']);
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);
$country = file_get_contents('http://api.wipmania.com/' . $ipusuari);
//$country = file_get_contents('http://api.hostip.info/country.php?ip='.$_SERVER['REMOTE_ADDR'];
//echo $country; 
$fp = fopen("fitxer.txt", "a+");
$usuari = date("Ymd G:i:s") . "\t" . $_SERVER{'REMOTE_ADDR'} . "\t" . $_SERVER{'HTTP_X_FORWARDED_FOR'} . "\t" . $hostusuari . "\t" . $country . " " .  $two_letter_country_code . "\t" . $_SERVER['PHP_AUTH_USER'] . "\t" .  $_SERVER['PHP_AUTH_PW'] . "\t" . $_SERVER{'DOCUMENT_ROOT'} . "\t" . $_SERVER{'REQUEST_URI'} . "\t" . $_SERVER{'REDIRECT_URL'} . "\t" . $info_usuari . "\n";
fwrite($fp, $usuari);
fclose($fp);
</syntaxhighlight>


"a+" enlloc de "w+" serveix per afegir al final del fitxer.
I si no s'ha calculat el pais amb la base de dades de fitxers:
<syntaxhighlight lang="php">
$two_letter_country_code=iptocountry($_SERVER['REMOTE_ADDR']);
function iptocountry($ip) {   
    $numbers = preg_split( "/\./", $ip);   
    include("ip_files/".$numbers[0].".php");
    $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);   
    foreach($ranges as $key => $value){
        if($key<=$code){
            if($ranges[$key][0]>=$code){$two_letter_country_code=$ranges[$key][1];break;}
            }
    }
    if ($two_letter_country_code==""){$two_letter_country_code="unkown";}
    return $two_letter_country_code;
}
</syntaxhighlight>

Revisió de 19:04, 9 maig 2012

Llegir i posar en Array per linies

Maxim fitxers de 10MB.

$lines = file("myfile.txt");
foreach($lines as $line) {
   echo($line);
}
echo $line[0];

o també:

$lines = file('http://www.example.com/');


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.


Lock file

        $fp = fopen($COUNT_FILE, "w");           //open the file for write
        while (flock($fp, LOCK_EX) == false)  // do an exclusive lock
        {usleep(rand(1, 600));}

        fputs($fp , "$visits[0]");           //write the new value to the file

        flock($fp, LOCK_UN); // release the lock
        fclose($fp);            //close the file


Check Size and rename

 date_default_timezone_set('CET');
 if (is_file($logaccess)){
	if (filesize($logaccess) > 500000)
	{
	rename($logaccess, $logaccess . "." . date("ymdHis") . ".txt");
	}
 }

Afegir a un registre

       date_default_timezone_set('CET');
	$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

date_default_timezone_set('CET');

$elim = array("\n", "\r", "\t", '$', "\\'", "'", "\\");

$info_usuari  = str_replace($elim,' ',$_SERVER['HTTP_ACCEPT_LANGUAGE']) . "\t";
$info_usuari .= str_replace($elim,' ',$_SERVER['HTTP_USER_AGENT']) . "\t";
$info_usuari .= str_replace($elim,' ',$_SERVER['HTTP_ACCEPT']) . "\t";
$info_usuari .= str_replace($elim,' ',$_SERVER['HTTP_ACCEPT_ENCODING']) . "\t";
$info_usuari .= str_replace($elim,' ',$_SERVER['HTTP_ACCEPT_CHARSET']) . "\t";
$info_usuari .= str_replace($elim,' ',$_SERVER['HTTP_REFERER']);

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);

$country = file_get_contents('http://api.wipmania.com/' . $ipusuari);
//$country = file_get_contents('http://api.hostip.info/country.php?ip='.$_SERVER['REMOTE_ADDR'];
//echo $country;  


 $fp = fopen("fitxer.txt", "a+");
 $usuari = date("Ymd G:i:s") . "\t" . $_SERVER{'REMOTE_ADDR'} . "\t" . $_SERVER{'HTTP_X_FORWARDED_FOR'} . "\t" . $hostusuari . "\t" . $country . " " .  $two_letter_country_code . "\t" . $_SERVER['PHP_AUTH_USER'] . "\t" .  $_SERVER['PHP_AUTH_PW'] . "\t" . $_SERVER{'DOCUMENT_ROOT'} . "\t" . $_SERVER{'REQUEST_URI'} . "\t" . $_SERVER{'REDIRECT_URL'} . "\t" . $info_usuari . "\n";
 fwrite($fp, $usuari);
 fclose($fp);


I si no s'ha calculat el pais amb la base de dades de fitxers:

$two_letter_country_code=iptocountry($_SERVER['REMOTE_ADDR']);
function iptocountry($ip) {    
    $numbers = preg_split( "/\./", $ip);    
    include("ip_files/".$numbers[0].".php");
    $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);    
    foreach($ranges as $key => $value){
        if($key<=$code){
            if($ranges[$key][0]>=$code){$two_letter_country_code=$ranges[$key][1];break;}
            }
    }
    if ($two_letter_country_code==""){$two_letter_country_code="unkown";}
    return $two_letter_country_code;
}