Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 916377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:02:30+00:00 2026-05-15T18:02:30+00:00

Is there any alternative to file_get_contents that would create the file if it did

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:02:31+00:00Added an answer on May 15, 2026 at 6:02 pm
    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;
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

are there any alternative to transactionScope which does not need to enable DTC?? In
Is there any alternative to this: Organizations.Include(Assets).Where(o => o.Id == id).Single() I would like
Is there any generic alternative / implementation for MemoryCache? I know that a MemoryCache
Is there any alternative to Axis2? Or the way to make it work (different
Are there any alternative to xlrd, xlwt and xlutils for handling MS Excel in
Is there any alternative to use like tag to property of UIView? The thing
Is there any similar peek(); (From C++) function in ruby? Any alternative to do
As chrome.browserAction.onClicked works only if there is no popup, is there any alternative method
How do you SortDictionary by value (.NET 2.0)? Or is there any alternative to
Is there any javascript alternative to Cronjob? The thing is, my boss doesn't want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.