Solved

file_get_contents dieing

Posted on 2014-10-17
16
84 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
Comment Utility
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
Comment Utility
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
Comment Utility
Have you got errors enabled

Top of the page
error_reporting(E_ALL);
0
 
LVL 9

Expert Comment

by:Brian Tao
Comment Utility
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 108

Expert Comment

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

Expert Comment

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

Author Comment

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

Author Comment

by:tonelm54
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 58

Expert Comment

by:Gary
Comment Utility
Can you attach the file here.
Copy viewTable.js to notepad and resave it.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
How long does this script run?
0
 

Author Comment

by:tonelm54
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
"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
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
Good, and thanks for the points, ~Ray
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now