Solved

file_get_contents dieing

Posted on 2014-10-17
16
86 Views
Last Modified: 2014-10-20
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?
0
Comment
Question by:tonelm54
  • 6
  • 5
  • 4
  • +1
16 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40386859
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
 

Author Comment

by:tonelm54
ID: 40386898
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
 
LVL 58

Expert Comment

by:Gary
ID: 40386910
Have you got errors enabled

Top of the page
error_reporting(E_ALL);
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 9

Expert Comment

by:Brian Tao
ID: 40386913
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40386936
Yep, "View source" is your friend!
0
 
LVL 58

Expert Comment

by:Gary
ID: 40386948
View source is irrelevant - what happened to
echo "Ended";
0
 

Author Comment

by:tonelm54
ID: 40386980
I have 'viewed source' but got nothing, but like Gary said it should still display 'Ended'
0
 

Author Comment

by:tonelm54
ID: 40386990
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
 
LVL 58

Expert Comment

by:Gary
ID: 40387001
Can you attach the file here.
Copy viewTable.js to notepad and resave it.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40387037
How long does this script run?
0
 

Author Comment

by:tonelm54
ID: 40387071
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40387081
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40387095
"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
 

Author Comment

by:tonelm54
ID: 40392004
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
 

Author Comment

by:tonelm54
ID: 40392010
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40392104
Good, and thanks for the points, ~Ray
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question