Increasing a number on the first line of a file by 1 with php -


i have script counting number of downloads of file downloaded internet. has log file adds line of text every time file downloaded.

i want modify first line number, , every time file downloaded increases number 1 (so it's easy see how many people have downloaded it.)

here's code have right (this isn't working):

$conten = @file_get_contents(log_file); //first line: $conten[0];  $content = fgets($conten);  $fo = @fopen(log_file, 'r+'); if ($fo) {   $content++;   @fputs($fo, ".$content.");   @fclose($fo); }   $f = @fopen(log_file, 'a+'); if ($f) {   @fputs($f, date("m.d.y g:ia")."  ".$_server['remote_addr']."  ".$fname."\n");   @fclose($f); } 

the $f part works fine...it's parts above aren't working want.

thanks!

file_get_contents() sucks entire file string. try fgets() on string, incorrect - fgets() works on filehandles, not strings. if weren't suppressing errors @ (never idea), you'd have seen php warn this. never supress errors, while developing. it's playing sports , saying "who cares if broke leg, i'm going run marathon now"/.

i'd suggest use database kind of thing. simple

update downloads set total = total + 1 file_id = xxx 

is far safer doing these file operations. if log file grows large. you'd sucking entire multi-megabyte file memory, reading 1 value, dumping out. again opening , rewriting file. if 2 or more downloads complete @ same time, you're going trash log file conflicting reads/writes.

if insist on file-based operation, @ using flock() restrict access other parallel downloads while script doing updates.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -