0

I haven't been able to find a solution similar to my problem.

I have a file on a server (UNIX). The file is in a readable format on the server (ie includes spaces, tabs, and newlines as ^M).

If I var_dump() the file it keeps the format but if I echo it removes all whitespace and newlines. Is there a way to display this file the same as it would be viewed on the server? below is the way I was attempting to do this:

if($login){
    $file = 'ftp://'.$username.':'.$password.'@'.$server.$path;
    $contents = file_get_contents($file);
    var_dump($contents);//is a string
    $contents = str_replace("^M","<br>",$contents);
    echo $contents;
}
FamousAv8er
  • 2,345
  • 2
  • 9
  • 27

1 Answers1

2

PHP isn't removing the spaces, the browser is doing it because you're echoing into an HTML web page. You need to tell the browser not to reformat it.

$contents = nl2br($contents);
echo "<pre>$contents</pre>";

You should also use the built-in nl2br() function to add <br> tags, rather than str_replace(). Although this isn't really needed inside <pre> tags.

Barmar
  • 741,623
  • 53
  • 500
  • 612