0

I am trying to notify a user, the status of a (another)script the runs in background (it runs once every minute).

So I have this php code:

    while(@fopen("dl.conf","r")){ 
      print "Download will start soon";
      flush();
      sleep(1);
    }
    .
    .

It doesn't print anything, plus even if I cancel that file it won't execute the rest of the script.

basically a user can submit a file to download. this will trigger the file dl.conf to be created. there is a script running in background checking if dl.conf exists, if so it reads it, and starts the download. It will delete dl.conf file too.

It might happen that the user submit the download but the script will take few more seconds to run again, and realize that there is something to download. so I just want the user to wait for the download which will start very soon (then the download progress will be shown).

thank you

3
  • There's a parse error in your code. Commented Sep 15, 2011 at 16:51
  • 2
    There is no parse error, I just ran this code and no error is shown Commented Sep 15, 2011 at 16:55
  • weird I don't get any parse error. Could you please tell me what the error is? Commented Sep 15, 2011 at 16:56

3 Answers 3

0

Well you are blocking the error.

Try this:

$fOpen = @fopen("dl.conf","r");

if(!$fOpen) die('ERROR of some kind...');

while($fOpen){ 
  print "Download will start soon";
  flush();
  sleep(1);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I'm sorry but I get the same behavior this way. Nothing is printed.
@Danny -- do you have php errors and error logging turned on?
yes, I just checked, I have display_errors and log_errors ON. error_reporting = E_ALL | E_STRICT
@Danny remove the @ before the fopen
if I remove @ nothing change. plus,I am noticing that the dl.conf won't be ever deleted this way. It is open and it needs a close()
0

you'd better to use ajax for this case and also change this script which should be handled in ajax onresponse event

if(file_exists('dl.conf'))
{
 return 'file created, download will start soon';
}

3 Comments

It is actually handled in ajax I forgot to mention it. This code is part of the script that shows the progress bar. Anyway, I need a cycle or something that won't let the script go on, otherwise it will replace whatever message I write with something else.
the response will be buffered and wont be sent to output while script is still running, you can try to play with ob_flush() function or related configuration options in php.ini but i think it is not the option
also possibly this question can be helpful stackoverflow.com/questions/6385676/…
0

You do not need to use fopen() to check if file is there, you can simply use file_exists() like this.

// run loop till file exists
while(file_exists("dl.conf")){ 
    print "Download will start soon";
    flush();
    sleep(1);

    // clear stat cache to get correct file status
    clearstatcache();
}

You do not need the clearstatcache() call if the background script is also a PHP script and uses unlink() to delete the dl.conf file. As unlink() clears the file stat cache automatically.

Update

Sometimes flush() or ob_flush alone do not work. Look at this comment on PHP documentation http://php.net/manual/en/function.ob-flush.php#90529

So if you are using output_buffering for sure, you can try this set of commands.

ob_end_flush(); 
ob_flush(); 
flush(); 
ob_start(); 

9 Comments

I'm sorry but I get the same behavior this way. Nothing is printed. I cannot use unlink() because this php script is not in charge to delete dl.conf. It doesn't know when to delete it
If you get no output, it means that file_exists() is returning false. try var_dump(file_exists("dl.conf")) just before the while loop to check what is the return value.
what is the output of var_dump(file_exists("dl.conf"))
boolean true, when the file exists. false otherwise. Anyway I had to remove the while loop in order to print. even if I placed it before the loop.
If the output is true, then the while() loop should run. If it is not executing even once, it means the dl.conf file is already deleted by the upload script before the while() loop is reached.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.