Page 1 of 1

openf writes file as 0 doesn't get any errors or warnings

Posted: Thu May 14, 2009 1:24 pm
by chuy244
Hi!
I'm traing to write a file but when i run the script I get a file calle "0" with no extension, the weird thing is that I don't get any warnings or erros. I don't know what I'm doing wrong... I've already looked in several pages, but I don't seem to find any answer to my problem. The php script is in: http://src.cimaco.com.mx/textfile/file_test.php it is supposed to write a file called v.ip within a folder called polldata at document level, but it only writes a file called "0" at document level.

Here de code:

Code: Select all

<?
    //Recuperamos la IP del usuario
            $ip = getenv("REMOTE_ADDR");
            //Hora actual
            $this_time = time();
            // Revisamos si existe el archivo de las IP's
            if (is_file($_SERVER['DOCUMENT_ROOT']+"/polldata/v.ip"))
            {
                echo("Se encotró el archivo<br>");
                //Arreglo que guarda todas las direcciones IP de los usuarios
                $ip_array = file($_SERVER['DOCUMENT_ROOT']+"/polldata/v.ip");
                //Abrimos el archivo de las IP para escritura y los truncamos
                $ip_table = fopen($_SERVER['DOCUMENT_ROOT']+"/polldata/v.ip","wb") or exit("No se pudo abrir el arvhivo");
                echo ("ip_table: ");
                echo $ip_table;
                echo "<br>";
                flock($ip_table, 2);
                //Se barre el arreglo de las Ip's para revisar la existencia o no de la IP actual
                echo "tamaño de (ip_array): ";
                echo sizeof($ip_array);
                for ($i=0; $i<sizeof($ip_array); $i++)
                {
                    //Extraemos el valor de la linea actual del archivo en 2 variables
                    list ($ip_addr, $time_stamp) = split("\\|",$ip_array[$i]);
                    //Revisamos si el intervalo de tiempo es menor
                    if ($this_time < ($time_stamp+3600*24))
                    {
                        //Revisamos si la IP existe
                        if ($ip_addr == $ip) 
                        {
                            continue;
                        }
                        
                        fwrite($ip_table,"$ip_addr|$time_stamp");
                    }
                }
                //Si el archivo esta vacio
                fwrite($ip_table,"$ip|$this_time\n");
                flock($ip_table, 3);
                fclose($ip_table);
            }
            else
            {
                //Si el archivo no esta creado
                $ip_table = fopen($_SERVER['DOCUMENT_ROOT']+"/polldata/v.ip","wb") or exit("No se pudo abrir el arvhivo");
                fwrite($ip_table,"$ip|$this_time\n");
                fclose($ip_table);
            }
?>

Re: openf writes file as 0 doesn't get any errors or warnings

Posted: Thu May 14, 2009 3:04 pm
by Darhazer
Use '.' for concatenation, not '+'

Code: Select all

is_file($_SERVER['DOCUMENT_ROOT']."/polldata/v.ip"
With +, you are performing addition and arguments are casting to numbers

Re: openf writes file as 0 doesn't get any errors or warnings

Posted: Fri May 15, 2009 9:38 am
by chuy244
Darhazer wrote:Use '.' for concatenation, not '+'

Code: Select all

is_file($_SERVER['DOCUMENT_ROOT']."/polldata/v.ip"
With +, you are performing addition and arguments are casting to numbers
¬¬ LOL... Thanks a lot...!!! It was driving me crazy :banghead: ..!! Problem solved :D