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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:40:52+00:00 2026-06-18T11:40:52+00:00

My problem here is that i don’t know how to properly write a file

  • 0

My problem here is that i don’t know how to properly write a file using fwrite. I parse xml using simplexml and then, while echo works fine, i can’t write the same result in a file. In this example below, i echo 5 items from an xml feed, but when i write it to a file, it writes the 5th item only. Can anyone tell what’s the problem?

$file = simplexml_load_file($url); {
$c = 0;
foreach($file->channel->item as $post) {
if($c == 5){
break;
}

$headline = (string) $post->title;

echo $headline;

$fp=fopen('myfile.htm', "w+");  
fwrite($fp, $headline."<br />");
fclose($fp);
        $c++;
        }
}

EDIT : Second attempt . I used a while loop, instead of if, but i only managed to write 5 times the 5th item. I need w+ (and not a+) to overwrite file every time it is updated and not add content each time:

$file = simplexml_load_file($url); {

foreach($file->channel->item as $post) {

$fp=fopen('myfile.htm', "w+");
$c = 1;
while ($c <= 5){
    $headline = (string) $post->title;
    fwrite($fp, "$headline<br />");
    $c++;
}
fclose($fp);
}
include 'myfile.htm';
}
  • 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-18T11:40:53+00:00Added an answer on June 18, 2026 at 11:40 am

    You can either open the file outside the loop, or if you must open it inside, open it with a mode.

    $fp = fopen('myfile.htm', 'w+');
    foreach(...) {
        //write here
    }  
    fclose($fp);
    

    or

    foreach(...) {
        $fp = fopen('myfile.htm', 'a');
        //write here
        fclose($fp);
    }
    

    What the w+ mode does is open the file in write mode, and points to the beginning of the file, which results in only the last iteration being written in the file. All the previous iterations are overwritten.

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

Sidebar

Related Questions

date here my problem: String datetime = 2012-03-24 23:20:51; I know that that string
My problem here is that I don't have the vocab to search for examples
my problem here that i don't understand how does the method called objectchanger works
My problem here that i don't understand why this override do not work here
I think there must be something subtle going on here that I don't know
I have a problem here that requires to design a data structure that takes
I built a linear layout inside of a dialog , the problem here that
My problem in here ( probably ) is that $db->fetch_array won't show all the
Updated Problem solved, I have some design problem here. The directory looks like that:
So here is the problem that I am facing: I have an application that

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.