Jump to content

PHP filesystem


mortalc

Recommended Posts

Just a few questions about PHP filesystem and PHP in general.

  1. Will file_get_contents open a file automatically, or must it already be opened?
  2. Is it possible to use PHP in javascript custom functions?
  3. How would I get a piece of PHP code (probably one with AJAX), to execute everytime a speicified file changes (e.g. editted by someone else)?
  4. Also, will it work anywhere on the page? It will qrite the contents of the editted file, but in a specific place - this means that it won't be at the end of the whole script, but somewhere in the middle.
  5. Is it possible to have all pages redirect to a specific one? e.g. attempting to access a text file without being logged on?

Link to comment
Share on other sites

1. Yes. The whole idea of file_get_contents is to open a file for reading, read all of its contents, return it as a string, and close the file. If the file is already opened by something else, a new "handle" will be created for reading during the execution of file_get_contents.2. No. PHP and JavaScript run at different times. It is however possible to send a new request from JavaScript to PHP via the so called "AJAX" (a.k.a. the XMLHttpRequest object) which creates this sort of illusion... See W3Schools AJAX tutorial for details.3. Your host needs to support cron jobs and have the libevent extension installed. If that's there, you can write a script that will run continiously on the server and perform any changes that need to happen on such events... and no, there's no other way... not really at least (you may be able to achieve a pseudo something like this if you constantly make checks in your normal scripts, but that's far from efficient).4. Huh? Clarify please.5. Yes, but that's not exactly PHP's job... it's the web server's job. You can do URL rewriting in which ALL files are mapped over a PHP file. The user will see the URL of the file, but PHP will be the thing that really executes. Once PHP is executed, it can check if the user is logged in, and pass over the file if they are and prompt for authentication if they're not. Authentication itself could be prompted at the same URL or after a redirection to another URL.

Link to comment
Share on other sites

1. Good.2. Oh - can I make a custom PHP function that can be called upon in the same way?3. Oh dear. In that case, can I have it check the file every second or so? ED 2: Basically is there a way to execute a PHP script/XMLhttprequest every few seconds or so?4. Basically here is the layout of the page:

// text file X written here//* Check for changes to file Y every second/on a button click (custom function with PHP)//* Write changes to file Y here// Form with text input here (will be written to Y)

I just want to know if the parts marked with a * will continue to run, even while the form is being used.5. That's Ok, it wasn't a major issue.ED: I've found the PHP article on functions, so ignore no. 2

Link to comment
Share on other sites

3. Yes, but this means that changes will be monitored only as long as the page containing the XMLHttpRequest is opened. As soon as it's closed, there will be no checks until it's opened again. If in between there was more than one change, the event handler will be executed only once (when the page is reopened), despite the fact there were multiple changes in the meantime.4. That's not a layout... it's a basic algoritm pseudocode... but it's not at all how this would work, due to the concurrent/asnychronous nature of the whole thing.What exactly are you trying to do? There are multiple approaches to this, and providing some context will help organize things the best.

Link to comment
Share on other sites

Sorry about that. I've done a quick bit of research and discovered a few things. I was asking the wrong questions:1. If I use setSeconds(0), will it still count the seconds? e.g.

<script type="text/javascript">var d = new Date();d.setSeconds(1);// Delay for two secondsdocument.write(d);</script

Will it output 0 or 2?2. If there is a PHP loop going on, will the rest of the code continue to execute, or will it have to wait until the loop is finished?

Link to comment
Share on other sites

1. Check out JavaScript timing to see how to use intervals or timeouts.The "d" variable contains the date and time at the point where the variable was initialized... it doesn't change as time goes by. You can change different portions of it by using the various set*() methods. In this case, regardless of the second, the output will contain "1" as a second, since that's what you're setting.Note that functions set with setInterval and setTimeout run asynchronously i.e. the code that follows the setInterval or setTimeout keeps running even before the functions in those methods are executed.2. Depends... if the XMLHttpRequest is asynchronous, the rest of the JavaScript code on that thread will continue to execute. If it's synchronous, it will block the thread until the PHP file is completely finished. You can tell XMLHttpRequest whether to be asynchronous or synchronous when you call the open() method.

Link to comment
Share on other sites

Final Question (I hope):Can I put a custom PHP function in a setTimeout function? I know you said I couldn't put PHP in a custom Javascript function, so I was just wondering if it would work the other way around.

Link to comment
Share on other sites

setTimeout/setInterval is still part of JavaScript - it sets a JavaScript function to run at a certain timeout/interval.

Link to comment
Share on other sites

So is there no way to call a PHP function in a setTimeout function?Here is basically what my page is (minus the form):

<html><head><?php$lastpost=nl2br(htmlspecialchars(file_get_contents("lastpost.txt")));function checkpost(){$new_last_post=nl2br(htmlspecialchars(file_get_contents("lastpost.txt")));if ($lastpost!==$new_last_post) echo $new_last_post;$lastpost=nl2br(htmlspecialchars(file_get_contents("lastpost.txt")));return $lastpost;}?><script type="text/javascript">var s=0;var t;function timer(){document.getElementById('txt').value=c;s=s+1;t=setTimeout("timer()",1000);checkpost()}</script></head><body><?phpecho file_get_contents("posts.txt");?><script type="text/javascript">timer();</script></body></html>

Link to comment
Share on other sites

Would you please stop trying to mix PHP and JavaScript?Just do a "View Source" on your page, and it should all become clear.Like I said, you can ALWAYS (with or without setInterval/setTimeout) make an XMLHttpRequest from JavaScript to PHP.

Link to comment
Share on other sites

as has been posted already, you can use AJAX to request/execute PHP scripts. The function referenced by setTimeout could be written to make the request to the PHP script and get some sort of data in response.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...