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

  • SEARCH
  • Home
  • 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 8927675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:16:17+00:00 2026-06-15T08:16:17+00:00

For a school assignment I have to write a script in PHP. It has

  • 0

For a school assignment I have to write a script in PHP. It has to create an array with names and write the array to a file. Then it has to delete the array, read the names in the file and the echo them.

This doesn’t work:

<?php
    $names = array("Name 1", "Name 2", "Name 3", "Name 4", "Name 5");
    $filename = "names.txt";
    $handle = fopen($filename, "wb+"); //open as read and write
    $string = implode("\r\n", $names); //name on each line
    fwrite($handle, $string); //write line

    $names = array(); //empty array
    print_r($names); //print empty array
    $string = fread($handle, filesize($filename)); //read file
    $names = explode("\r\n", $string); //lines to array
    foreach ($names as $name) {
        echo("<br>\n$name");
    }
    fclose($handle);
?>

But this works:

<?php
    $names = array("Name 1", "Name 2", "Name 3", "Name 4", "Name 5");
    $filename = "names.txt";
    $handle = fopen($filename, "wb"); //open as write
    $string = implode("\r\n", $names); //name on each line
    fwrite($handle, $string); //write line
    fclose($handle); //close

    $handle = fopen($filename, "r"); //open as read
    $names = array(); //empty array
    print_r($names); //print empty array
    $string = fread($handle, filesize($filename)); //read file
    $names = explode("\r\n", $string); //lines to array
    foreach ($names as $name) {
        echo("<br>\n$name");
    }
    fclose($handle);
?>

I don’t get why it works when I close and reopen the file. Any help would be appreciated.

  • 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-06-15T08:16:17+00:00Added an answer on June 15, 2026 at 8:16 am

    All you need is to restart the file pointer because when using fwrite():

    fwrite() writes the contents of string to the file stream pointed to by handle.

    • length bytes have been read
    • EOF (end of file) is reached
    • a packet becomes available or the socket timeout occurs (for network streams)
    • if the stream is read buffered and it does not represent a plain file, at most one read of up to a number of bytes equal to the chunk size (usually 8192) is made; depending on the previously buffered data, the size of the returned data may be larger than the chunk size.

    if you look at fread()

    fread() reads up to length bytes from the file pointer referenced by handle. Reading stops as soon as one of the these conditions is met:

    So at at the time you are calling fread($handle, filesize($filename)); you are already at the end of file therefore nothing would be displayed.

    To resolve this just use fseek to restart the pointer back to the beginning of the file

    Example :

     fseek($handle, 0);
    

    Yon’t don’t need to close the file .. your first code would work perfectly

    Edit

    You can also you rewind from PHP DOC

    Sets the file position indicator for handle to the beginning of the file stream.

     rewind($handle);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a school assignment I have to create a C++ program that will create
Hello is have a question for a school assignment i need to : Read
I have a school assignment where I need to create a data-driven style of
We have to create an application for iPhone and Android as a school assignment.
I am creating a file server application for school assignment. What I currently have
Right now I have a school assignment where I have to create a connect
We have an assignment in school where we got a header file and we
For a school assignment I have to write x86 assembly code, except I can't
I currently have a school assignment that involves both PHP and asp.net. Now the
I have to implement a search algorithm for a school assignment. Right now, i'm

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.