encoree1337
encoree1337

Reputation: 337

PHP corrupted readed file

my php file looks like:

$file = file("test.cpp");
//checks if file is opened properly...
foreach($file as $line)
{
  echo $lines."<br/>";
}

test.cpp file looks like:

#include <iostream>

int main()
{
  int a,b,c;
  cin>>a>>b;
  cout<<a;
  return 0;
}

but printed output - look at it:

#include

int main()
{
int a, b;
float x,y,z;
cin>>a>>b;
cout<return 0;
} 

it is corrupted, but I have no idea why - original file is in valid state, but only loaded lines are sometimes corrupted

Upvotes: 1

Views: 54

Answers (1)

Cheery
Cheery

Reputation: 16214

$file = file("test.cpp");
//checks if file is opened properly...
echo '<pre>';
foreach($file as $line)
{
  echo htmlentities($line);
}
echo '</pre>';

ps: no need in <br> tag, <pre> preserves new line. or just

if (file_exists("test.cpp"))
  echo '<pre>' . htmlentities(file_get_contents("test.cpp")) . '</pre>';

Upvotes: 3

Related Questions