file_get_contents()如果内部包含“<body>”,则无法读取文件

I am uploading a .json file to server and using file_get_contents(), reading the content out of it. The file contains "<body>" as a value of a json key. The file_get_contents() function is returning a

<script type="text/javascript">...</script>

instead of the string.

  • I have tried mb_convert_encoding($string, 'HTML-ENTITIES', "UTF-8");
  • I have tried htmlentities()

  • I have tried to simple fopen read mode and output

  • I have triedmb_convert_encoding($content, 'UTF-8',mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));

I tried simple text "abc<body>dfg" in a text file and do file_get_contents(). All are failing and giving same output. Any help will be appreciated. I cannot use cURL as the file resides out of my root dir.

try{
    if (0<$_FILES['file']['error']){
             echo 'Err: ' . $_FILES['file']['error'] . '<br>';
     }else{
          $filename = time() . $_FILES['file']['name'];
            move_uploaded_file($_FILES['file']['tmp_name'], '/tmp/'.$filename);
     }
     echo file_get_contents('/tmp/'.$filename);
}catch (Exception $e){
        echo $e->getMessage();
}

enter image description here

The file contains string "blah blah <body> blah blah" Output : blah blah <script>Some JS Code</script><body> blah blah

You are outputting RAW file content to a browser. Use var_dump(file_get_contents('/tmp/'.$filename)) instead.

This JavaScript code belongs to New Relic monitoring system. It code gets appended automatically by a PHP/Apache module as long as it's active on your server. Check out the server configuration and disable it, if you manage server by yourself, otherwise contact hosting support team.