tags:

views:

46

answers:

2

Is there any alternative to file_get_contents that would create the file if it did not exist. I am basically looking for a one line command. I am using it to count download stats for a program. I use this PHP code in the pre-download page:

Download #: <?php $hits = file_get_contents("downloads.txt"); echo $hits; ?>

and then in the download page, I have this.

<?php
    function countdownload($filename) {
        if (file_exists($filename)) {
            $count = file_get_contents($filename);
            $handle = fopen($filename, "w") or die("can't open file");
            $count = $count + 1;
        } else {
            $handle = fopen($filename, "w") or die("can't open file");
            $count = 0; 
        }
        fwrite($handle, $count);
        fclose($handle);
    }

    $DownloadName = 'SRO.exe';
    $Version = '1';
    $NameVersion = $DownloadName . $Version;

    $Cookie = isset($_COOKIE[str_replace('.', '_', $NameVersion)]);

    if (!$Cookie) {
        countdownload("unqiue_downloads.txt");
        countdownload("unique_total_downloads.txt");
    } else {
        countdownload("downloads.txt");
        countdownload("total_download.txt");
    }

    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$DownloadName.'" />';
?>

Naturally though, the user accesses the pre-download page first, so its not created yet. I do not want to add any functions to the pre download page, i want it to be plain and simple and not alot of adding/changing.

Edit:

Something like this would work, but its not working for me?

$count = (file_exists($filename))? file_get_contents($filename) : 0; echo $count;
A: 
Download #: <?php
$hits = '';
$filename = "downloads.txt";
if (file_exists($filename)) {
    $hits = file_get_contents($filename);
} else {
    file_put_contents($filename, '');
}
echo $hits;
?>

you can also use fopen() with 'w+' mode:

Download #: <?php
$hits = 0;
$filename = "downloads.txt";
$h = fopen($filename,'w+');
if (file_exists($filename)) {
    $hits = intval(fread($h, filesize($filename)));
}
fclose($h);
echo $hits;
?>
kgb
This is better than creating handles and such for my function in the download page, but I was hoping for short and simple solution, but I guess ill just need to have big long code for the download count in the pre-download.
BHare
now that i think of it, your way to count downloads is quite strange;) why don't you use a database?
kgb
@kgb just needed a very simple solution
BHare
A: 

type juggling like this can lead to crazy, unforeseen problems later. to turn a string to an integer, you can just add the integer 0 to any string. example: $f = file_get_contents('file.php'); $f = $f + 0;

echo is_int($f); //will return 1 for true

however, i second the use of a database instead of a text file for this. there's a few ways to go about it. one way is to insert a unique string into a table called 'download_count' every time someone downloads the file. the query is as easy as "insert into download_count $randomValue" - make sure the index is unique. then, just count the number of rows in this table when you need the count. the number of rows is the download count. and you have a real integer instead of a string pretending to be an integer. or make a field in your 'download file' table that has a download count integer. each file should be in a database with an id anyway. when someone downloads the file, pull that number from the database in your download function, put it into a variable, increment, update table and show it on the client however you want. use PHP with jQuery Ajax to update it asynchronously to make it cool.

i would still use php and jquery.load(file.php) if you insist on using a text file. that way, you can use your text file for storing any kind of data and just load the specific part of the text file using context selectors. the file.php accepts the $_GET request, loads the right portion of the file and reads the number stored in the file. it then increments the number stored in the file, updates the file and sends data back to the client to be displayed any way you want. for example, you can have a div in your text file with an id set to 'downloadcount' and a div with an id for any other data you want to store in this file. when you load file.php, you just send div#download_count along with the filename and it will only load the value stored in that div. this is a killer way to use php and jquery for cool and easy Ajax/data driven apps. not to turn this into a jquery thread, but this is as simple as it gets.

hyperlexic