Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
LOUOS

php local file reading shenanigans

Recommended Posts

Posted · Original PosterOP

Hi, so for the last three hours I have been trawlling through youtube videos and forums trying to find out a way of reading a .txt file and storing it as a javascript variable. Initially I was hoping for something similar to pythons .open and .read procedure in javascript. However what I found out was that you have to do that in php then "send it" to the javascript so this is what I've come up with:

 


<!DOCTYPE html>
<html>
<body>
<script>
function Number(){
    var x = "<?php echo $myfile;?>";
    alert(x);
}
window.addEventListener('load',Number);
</script>
<?php 
$rawfile = fopen("obstacles.txt", "r");                  // obstacles contains 'I hate php'
$myfile = fread($rawfile,filesize("obstacles.txt"));
?>

</body>
</html>

 

The alert always just contains <?php echo $myfile;?> as text (I hesitate to say string). Would appreciate help with problem. :)

 

PS. If theres anything about opening directly as an array that would be great.


temporary solution - someone else's problem

 

Link to post
Share on other sites

You'll have to do the file reading before the html code, otherwise the variable $myfile will be empty when the html code is sent to the output buffer.

 

You also have functions like file_get_contents($filename) which read a whole file and return a string that has the whole contents of the file (no need for multiple calls)

 

Also you should close the file handle using fclose($rawfile) if you want to be nice (though php would kill the handle as soon as the script finishes. It's just good to make it a habit to not leave things open

 

later edit:  if you don't want code at the top and you don't want to use file_get_contents you can do something like this

 

<!DOCTYPE html>
<html>
<body>
<script>
function Number(){
    var x = "<?php echo my_custom_function_name();?>";
    alert(x);
}
window.addEventListener('load',Number);
</script>
<?php 
  
function my_custom_function_name() {  
	$rawfile = fopen("obstacles.txt", "r");
 	$fileinfo = fstat($rawfile);
	$myfile = fread($rawfile,$fileinfo['size']);
  	fclose($rawfile);
  	return $myfile;
}
  
?>

</body>
</html>

 

Link to post
Share on other sites
Posted (edited) · Original PosterOP

this better?

<!DOCTYPE html>
<html>
<body>
<?php 
$myfile = file_get_contents("obstacles.txt");
?>
<script>
function Number(){
    var x = "<?php echo $myfile;?>";
    alert(x);
}
window.addEventListener('load',Number);
</script>
</body>
</html>
Edited by LOUOS

temporary solution - someone else's problem

 

Link to post
Share on other sites

No need to use fopen if you use file_get_contents  , file_get_contents does everything (opens file, reads everything , closes file, returns content or empty string if file is not found or error reading it).

 

Read documentation : http://docs.php.net/manual/en/function.file-get-contents.php

 

later edit: also keep in mind that fread doesn't guarantee that it will always read as much as you tell it. In some rare cases, the fread function may return a smaller amount than what you want or even 0 bytes, even if the file is bigger or there's still bytes left to read. It depends on operating system and other things. 

So the parameter size is just a "hint", a suggestion. For reading huge files, you should read chunks at a time (for example 64 KB pieces) and stop when both fread returns 0 bytes AND feof() function says you reached the end of file.

For example, if the text file is as big as 1 MB , the operating system or php may only read 64KB or 512 KB in one shot and will only return that much.

If you use fread to read a file, you would use a while not end of file  .. read chunk , add chunk to a string, read next chunk etc

Link to post
Share on other sites

You should not be using PHP to output a value into a Javascript variable like that.

 

If your Javascript requires a value from the server then you need to do an XMLHttpRequest (ajax) request to your PHP script which will return the required data in a data format that can be interpolated by Javascript most commonly used would be JSON.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×