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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:05:07+00:00 2026-05-17T15:05:07+00:00

I am looking for a (correct) way to read and write to a linux

  • 0

I am looking for a (correct) way to read and write to a linux text file with javascript, jQuery and php. Specifically, I want to take the value of a (#taFile) with jQuery ($(“#taFile”).val();) and $.post it to a php script, which in turn writes the posted value to a text file.

While my code mostly works, I have run into a snag: when taking the value of the textarea and $.posting it, all special characters are lost. Specifically, 1) all special characters are lost, 2) new lines are converted to a space and 3) all characters after certain special characters (I’ve noticed it happen with a #) are lost.

These are still rough drafts of the script however I’m very open to any improvements and suggestions!

Javascript:

$(document).ready(function() {
 $("input:button").button();
 $("#tabs").tabs();

 $("#tabs").tabs({
  select: function(event, ui) { // When a tab is selected
   file = ui.panel.id;
   console.log("js: file: "+file);
   if (file == "items" || file == "groups" || file == "whitelist" || file == "users"){ // if file is valid
    $("#taFile").remove(); // Remove any old textareas
    $("#"+file).html(''); // Add a new textarea
    $.post("/php/getFile.php?action=read&file="+file, function(data){ // Post with the current file
     $("#taFile").val(data); // Put file into textarea
    });
   }
  }
 });

 $("#btnSubmit").click(function() {
  var newcfg = $("#taFile").val();
  alert(newcfg); // This shows that newcfg preserves the exact value of the textarea
  console.log(newcfg); // As does this.
  $.post("/php/getFile.php?action=write&file="+file+"&newcfg="+newcfg); // This shows new lines (\n ?) as " " (a space) and removes all special characters (! , # ; : etc)
 });
});

PHP:

 $file = $_REQUEST['file'];
 $newcfg = $_REQUEST['newcfg'];
 $action = $_REQUEST['action'];
 $user = 'public';
 $config = "/home/$user/mc/$file.txt";

 if ($action == "read"){
  $oldcfg = file_get_contents($config);
  echo $oldcfg;
 } else if ($action == "write") { #if writing
  $fh = fopen($config, 'w') or die("Cant write to file"); #open file
  fwrite($fh, $newcfg); #write file
  fclose($fh); #close file
 }

I had to remove the php tags as they caused the actual PHP code to not show up. Not that some of the script is for reading the file in first, when changing tabs. That all works fine.

Thanks!

  • 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-17T15:05:08+00:00Added an answer on May 17, 2026 at 3:05 pm

    Note: You are using jQuery.post() and setting your data as a $_GET parameters. It should be called like;

    $.post("/php/getFile.php", { action: "write", file: file, newcfg: newcfg },
       function(data){
         alert("Data Loaded: " + data);
       });
    

    You can keep your line breaks with a function like this:

    function splitter($content) {
    $content = str_replace(chr(10),'<br/>',$content);
    $content = str_replace("<br/><br/><br/><br/>","<br/><br/>",$content);
    return $content;
    }
    

    Which you run like:

    $newcfg = splitter($newcfg);
    

    And typoknig is right abouit special chars

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

Sidebar

Related Questions

Looking for the correct way and control to import and export of out of
I'm looking for the correct way to export my pictures sequence into a quicktime
I am looking to the fastest and the correct way to check if a
I am looking at singletons and I was curious about the correct way to
I just read another users question while looking for a way to compute the
Is there a way to read a file's data but continue reading the data
I am looking for the best way to scale a PHP application under Nginx
I am looking for a way to read the ID3 tags from an MP3
I am looking for some advice. My situation: Application works with text local file
What's the correct way to resign the current firstResponder? I've seen the following: Looping

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.