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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:49:26+00:00 2026-05-30T17:49:26+00:00

I am very new to php, and i have search and put together this

  • 0

I am very new to php, and i have search and put together this script to convert text to csv and write the out put on the file.

      $File = "/var/apache2/htdocs/loginS/host.txt";
      $Handle = fopen($File,"r");
      $Content = fread ($Handle,filesize ($File));
       fclose($File);
       fclose($Handle);
       $Content = explode("\t", $Content);
       foreach($Content as $Value) {
                //echo $Value."|"; // till this line working 
       fwrite($save, $Value); 
       fclose($save); 
       }

the problem is when I try to write on the file. I got only one line.what is my error.

  • 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-30T17:49:27+00:00Added an answer on May 30, 2026 at 5:49 pm

    You are calling fclose() on the file in your loop that is writing records. fclose() closes the file handle so it is no longer valid and cannot be written to.

    Move fclose($save); after the } that ends the foreach() with your content.

    Also, you could simplify things a bit by calling $Content = file_get_contents($File); since that is what you are doing in effect with fread(). Also, since $File is just a string variable, calling fclose() on it is unnecessary and doesn’t do anything. You were correctly closing it by calling fclose() on $Handle. But using file_get_contents() will eliminate the need for both. The only file you would need is the one you were writing to.

    Here is an example using the file() function which reads each line of a file into an array.

    $file    = '/var/apache2/htdocs/loginS/host.txt';
    $content = file($file);
    
    $save    = fopen('./out.csv', 'w+');
    
    foreach($content as $line) {
        $line  = rtrim($line, "\r\n");  // remove the newline from $line
        $parts = explode("\t", $line);
    
        $lineCsv = implode('|', $parts); // or ',' ?
        fwrite($save, $lineCsv . "\n");  // write output to file
    }
    
    fclose($save);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very new to JS here. When I write PHP I have no problem assigning
I am very new to php and cannot find out how to fix this.
I am very new to PHP and have no idea why this is happening,
Very new to PHP. I have sucessfully got this piece of code working to
I'm very new to PHP and I have a small task of displaying a
I'm very new to php. I found some CMS like code for east text
I'm very new to PHP. I have a line of code on a server
I am very new to PHP, so far I have been using the following
I have a very simple test script: <?php $DSN = "mysql:host=db.example.edu;port=3306;dbname=search_data"; try { $DB
Im very new to php and i have a page with 5 includes located

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.