LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-25-2018, 04:24 AM   #1
regstuff
LQ Newbie
 
Registered: Oct 2018
Distribution: Ubuntu 16.04
Posts: 27

Rep: Reputation: Disabled
Php exec of cat command displays file contents without line breaks


I'm trying to list the contents of a file on a webpage. I'm using php to do a shell_exec of a cat command. The result seems to remove all line breaks. Cat on the commandline however displays the results appropriately.

On the command line:
Code:
cat /usr/local/nginx/scripts/1data.txt
Results in the below (with line breaks):

__stream2__out1__ rtmp://myserver.com
__stream1__out4__ rtmp://server2.com

However, on the webpage, I get the same but without line breaks.
__stream2__out1__ rtmp://myserver.com __stream1__out4__ rtmp://server2.com

The webpage php code is below.

Code:
<?php
if (isset($_GET['list'])) {
$output = shell_exec("cat /usr/local/nginx/scripts/1data.txt");
echo "$output";
}
?>
 
Old 11-25-2018, 04:52 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by regstuff View Post
I'm using php to do a shell_exec of a cat command.
this is very likely the wrong way to go about it.
i'm 99.99% sure that PHP has native functions for that.
 
Old 11-25-2018, 05:09 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,386
Blog Entries: 3

Rep: Reputation: 3775Reputation: 3775Reputation: 3775Reputation: 3775Reputation: 3775Reputation: 3775Reputation: 3775Reputation: 3775Reputation: 3775Reputation: 3775Reputation: 3775
As ondoho suggests, there are better ways to dump a file in PHP.

In general, if you are displaying plain text the MIME type needs be "text/plain" as in

Code:
Content-Type: text/plain\n\n
Or if you need HTML around it for the rest of the page, wrap the output of the file in a <pre> </pre> element.

https://www.w3.org/TR/html401/index/elements.html

https://www.iana.org/assignments/med...ia-types.xhtml

http://php.net/manual/en/funcref.php
 
1 members found this post helpful.
Old 11-25-2018, 08:08 AM   #4
regstuff
LQ Newbie
 
Registered: Oct 2018
Distribution: Ubuntu 16.04
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
this is very likely the wrong way to go about it.
i'm 99.99% sure that PHP has native functions for that.
As per your suggestion, modified the code to do this:
<?php
if (isset($_GET['list'])) {
echo file_get_contents( "../scripts/1data.txt" ); // get the contents, and echo it out.
}

But still getting output minus newlines.
 
Old 11-25-2018, 12:15 PM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,767

Rep: Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225
If you're echoing/dumping to a web page, note that newlines are not seen by web browsers. If you look at the page source, the newlines are probably there, but the browser is not going to show them.

You'll need to add a <br> tag in your code, or <pre></pre> as suggested by Turbocapitalist.

Last edited by scasey; 11-25-2018 at 12:16 PM.
 
Old 11-26-2018, 12:06 AM   #6
regstuff
LQ Newbie
 
Registered: Oct 2018
Distribution: Ubuntu 16.04
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
If you're echoing/dumping to a web page, note that newlines are not seen by web browsers. If you look at the page source, the newlines are probably there, but the browser is not going to show them.

You'll need to add a <br> tag in your code, or <pre></pre> as suggested by Turbocapitalist.
Thanks. pre tag worked.
$output = file_get_contents( "../scripts/1data.txt" );
echo "<pre>$output</pre>";
 
Old 11-26-2018, 06:28 AM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,767

Rep: Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225
Quote:
Originally Posted by regstuff View Post
Thanks. pre tag worked.
$output = file_get_contents( "../scripts/1data.txt" );
echo "<pre>$output</pre>";
Glad to have helped. You can use the Thread tools to mark this [SOLVED]...and you might want to acknowledge Turbocapitalist's contribution by clicking on the "Did you find this helpful" in their post.
 
  


Reply

Tags
cat, php



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Is there a command line way to print out all of the contents and sub directories contents of a directory? wh33t Linux - Software 5 06-07-2016 01:45 PM
Move file contents to another file with bash script (find, mimetype and cat) _furrycat_ Linux - Newbie 1 10-06-2013 11:12 AM
[SOLVED] less displays binary but cat displays ascii? ahtoot Linux - Newbie 3 01-25-2011 08:38 PM
how to put contents of register to line x using vim exec mode leocharre Linux - Software 3 03-19-2008 07:03 PM
list contents of directory without listing contents baddah Linux - Newbie 2 06-12-2006 04:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration