file_get_contents dieing

I want to be able to pull the contents of a file into a string, so using the following command:-
$return["html"] .= @file_get_contents("viewTable.js"); 

Open in new window


However the code dies after this line. I have tried putting it into a try/catch and also with error catching and ignore errors, but every time the code gets to this line it exists the script.

I have tried different ways of accessing the file but as soon as it tries to open it dies. Ive tried renaming the file all to no avail. Permissions are set for 'everyone' so not that issue either.

The contents of viewTable.js is simply:-

<script type="text/javascript">
$(document).ready(function () {
      $('#loggedinReport tr').click(function () {
            alert($(this).find("td:first").val());
      });
});
</script>

Any ideas what Im doing wrong?
tonelm54Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Ray PaseurConnect With a Mentor Commented:
Here is the PHP script I used that seems to work correctly.
<?php // demo/temp_tonelm54.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28539565.html

$return["html"] = "";
$return["html"] .= "Beginning";
if (file_exists('temp_tonelm54.js')) {
	$return["html"] .= "Here -1";
	$return["html"] .= file_get_contents("temp_tonelm54.js");
	$return["html"] .= "Here -2";
} else {
	$return["html"] .= "NOT FOUND!";
}
$return["html"] .= "End";

// SHOW WHAT HAPPENED
var_dump($return);

Open in new window

Here is the JavaScript file:
<script>alert('tonelm54');</script>

Open in new window

Here is the URL:
http://iconoun.com/demo/temp_tonelm54.php

When the JS file is not there, I get this:
array(1) {
  ["html"]=>
  string(22) "BeginningNOT FOUND!End"
}

Open in new window

Whe the JS file IS there, I get this (first):
array(1) {
  ["html"]=>
  string(61) "BeginningHere -1

Open in new window

Then after the JavaScript has executed, I see this:
array(1) {
  ["html"]=>
  string(61) "BeginningHere -1Here -2End"
}

Open in new window

And if I do "view source" I see this:
<pre>array(1) {
  ["html"]=>
  string(61) "BeginningHere -1<script>alert('tonelm54');</script>Here -2End"
}

Open in new window

Executive Summary: The browser's execution of the JavaScript interrupts the output from var_dump() and this is expected behavior.
0
 
GaryCommented:
Whats the error? Have you got error reporting turned on?

Is $return["html"] already set as an array since you are appending the file with .=

And using @ isn't good logic. You should be using file_exists then assign the file contents
0
 
tonelm54Author Commented:
Good afternoon Gary,
Yes, the $return['html'] contains data, but also tried setting to another variable, same issue.

Ive modified the code to now read:-
echo "BEGINNING";
if (file_exists('viewTable.js')) {
	echo file_get_contents("viewTable.js"); 
} else {
	echo "NOT FOUND!";
}
echo "Ended";

Open in new window


I just get:-

BEGINNING
nothing else, no errors or anything :-(
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
GaryCommented:
Have you got errors enabled

Top of the page
error_reporting(E_ALL);
0
 
Brian TaoSenior Business Solutions ConsultantCommented:
Are you viewing it as a web page? If so, the content will not be displayed in the browser, because it is a <script> block and is not supposed to appear on screen.
Did you try "View Source"?
0
 
Ray PaseurCommented:
Yep, "View source" is your friend!
0
 
GaryCommented:
View source is irrelevant - what happened to
echo "Ended";
0
 
tonelm54Author Commented:
I have 'viewed source' but got nothing, but like Gary said it should still display 'Ended'
0
 
tonelm54Author Commented:
So, what Ive got now is:-
$return["html"] = "";
error_reporting(E_ALL);
$return["html"] .= "Beginning";
if (file_exists('viewTable.js')) {
	$return["html"] .= "Here -1";
	$return["html"] .= file_get_contents("viewTable.js"); 
	$return["html"] .= "Here -2";
} else {
	$return["html"] .= "NOT FOUND!";
}
$return["html"] .= "End";

Open in new window


And the output is:-
Beginning
Here -1
0
 
GaryCommented:
Can you attach the file here.
Copy viewTable.js to notepad and resave it.
0
 
Ray PaseurCommented:
How long does this script run?
0
 
tonelm54Author Commented:
Im on the way home now, so will test on my home server and upload the files to here if still doesnt work.

Ray Paseur -takes a few milliseconds to load
0
 
Ray PaseurCommented:
"few millisecs" is good news - at least we know it's not timing out!  My guess is that the JS is being executed in the browser and you're seeing the pause because the JS is running when it's being displayed.  This is an object lesson in the theory of "escape output" which is why htmlentities() exists.  Consider an online forum where visitors can post messages.  What if one of the visitors posts something like this?
<script>/* Evil JavaScript */</script> Haha!  I just stole your cookies!

Open in new window

You would not want to send that to a client browser.  So instead you would use htmlentities() to escape the output string before sending it, thereby eliminating the risk that the JS would be executed on an unsuspecting client browser.
http://shiflett.org/blog/2005/feb/my-top-two-php-security-practices
0
 
tonelm54Author Commented:
Ok, very strange :-S

Printed the code, and typed in my code word by word, and works fine :-S

Copy and pasted code into my new script doesnt work. Compared it in and both are exactly the same, but the second one works :-S

Must be some hidden characters somewhere.
0
 
tonelm54Author Commented:
Unfortunately the idea is to fire some JavaScript back into the browser as its to setup the cell clicks on the returned table, and there is currently 83 different tables, so pointless putting every possible click in, just load the ones that are required when the user click on it.

Thank you for your assistance in this, like I say not really sure of the issue but working now
0
 
Ray PaseurCommented:
Good, and thanks for the points, ~Ray
0
All Courses

From novice to tech pro — start learning today.