PDA

View Full Version : Problem with .dat file created by php-script


Michiel
09-22-2002, 01:13 PM
Hi,

I've recently been working on a stats-script that uses .dat files for storing information. All the functions work correctly: the visitors are registered correctly. The problem is that my 'view-stats'-functions don't work because the file can't be opened. The seccond problem is that I can't edit or delete the datafile from my server using ws-ftp. So I think the problem lies in the fact a directory and a data-file are created when the script runs for the first time. I guess something goes wrong here... This is the specific code:


$dir = date("Y");

if (!is_dir($dir)) mkdir("$dir", 0775);


Is there anything wrong with these lines? Or does anybody have a clue why I can't edit/ delete the .dat file?

Thanx Michiel

firepages
09-22-2002, 01:43 PM
because your script created the file it is 'owned' by apache.

you should be able to <?unlink('filename.dat');?> the file though via script.

if your host allows it you can also CHOWN the file to your username <? CHOWN("user user filename.dat");?>
then you should be able to delete it via narmal method... but most shared hosts wont let you CHOWN

Michiel
09-22-2002, 01:57 PM
Hi firepages,

thanks for your reply. Unfortunately I don't understand exactly where to place the unlink-function. Do you know where to put it in the code?


function AddStat($table, $new_value, $unique_visit) {

$dir = date("Y");

if (!is_dir($dir)) mkdir("$dir", 0775);

if (file_exists("$dir/$table.dat")) $fp=file("$dir/$table.dat");

else $fp=Array();

$match=false;

foreach ($fp as $line)
{
list($value,$hits,$unique)=explode('|',trim($line));
$stat[$value]=Array('hits'=>$hits,'unique'=>$unique);
if ($value==$new_value) $match=true;
}

if ($match)
{
$stat[$new_value]['hits']++;
if (!$unique_visit == 1) $stat[$new_value]['unique']++;
}

else $stat[$new_value]=Array('hits'=>1,'unique'=>1);

if (!$fp = fopen("$dir/$table.dat", "w")) die("Couldn't open $dir/$table.dat for writing!");

foreach ($stat as $key=>$value)
{
$line="$key|{$value['hits']}|{$value['unique']}\r\n";
fputs($fp,$line,strlen($line));
}

fclose($fp);

}


Thanx Michiel