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 6011205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:13:35+00:00 2026-05-23T02:13:35+00:00

I want to make a .php file downloadable by my users. Every file is

  • 0

I want to make a .php file downloadable by my users.

Every file is different from an user to another:
at the line #20 I define a variable equal to the user ID.

To do so I tried this: Copy the original file. Read it until line 19 (fgets) then fputs a PHP line, and then offer the file to download.

Problem is, the line is not inserted after line 19 but at the end of the .php file. Here is the code:

if (is_writable($filename)) {

    if (!$handle = fopen($filename, 'a+')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    for ($i = 1; $i <= 19; $i++) {
        $offset = fgets($handle);
    }

    if (fwrite($handle, $somecontent) === FALSE) {
        exit;
    }

    fclose($handle);
}

What would you do ?

  • 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-23T02:13:35+00:00Added an answer on May 23, 2026 at 2:13 am

    append mode +a in fopen() places the handle’s pointer at the end of the file. Your fgets() loop will fail as there’s nothing left to read at the end of the file. You’re basically doing 19 no-ops. Your fwrite will then output your new value at the end of the file, as expected.

    To do your insert, you’d need to rewind() the handle to the beginning, then do your fgets() loop.

    However, if you’re just wanting people to get this modified file, why bother doing the “open file, scan through, write change, serve up file”? This’d leave a multitude of near-duplicates on your system. A better method would be to split your file into two parts, and then you could do a simple:

    readfile('first_part.txt');
    echo "The value you want to insert";
    readfile('last_part.txt');
    

    which saves you having to save the ‘new’ file each time. This would also allow arbitrary length inserts. Your fwrite method could potentially trash later parts of the file. e.g. You scan to offset “10” and write out 4 bytes, which replaces the original 4 bytes at that location in the original file. At some point, maybe it turns into 5 bytes of output, and now you’ve trashed a byte in the original and maybe have a corrupted file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make my php page only accessible from another page redirect and
Hy! I want to make a autosuggest from my php file that returns a
To allow caching a PHP generated file, I want to make sure, that the
I want to make a little php poll. The script should ask the users
I am newer for php. I want make php page cache, query data from
I want to make some thing like this my-php-file.php $lang = 'es'; my-js-file.js if
I want to make a call to some php file before that I'm trying
i want to make a php app that let people submit photos/videos/sounds Now, everything
I want to make a button that starts my php script after I click
I want to make sure people can't type the name of a PHP script

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.