Jump to content

Check for files (PHP) with XML response


kney

Recommended Posts

Hi,

 

I currently have this code:

 

<?php

function respond_xml($status, $message)
{
    $out = new XMLWriter();
    $out->openURI("php://output");
    $out->setIndent(true);

    $out->startDocument("1.0", "ISO-8859-1");

    $out->startElement("statuscheck");
    $out->writeElement("status", $status);
    $out->writeElement("message", $message);
    $out->endElement();

    $out->endDocument();
    
    $out->flush();
}

function main()
{
    header("Content-type: text/xml");

    if (!isset($_GET["file"]))
        respond_xml("ERROR", "File parameter missing");
    else if (file_exists($_GET["file"]))
            return respond_xml("OK", "File exists");
    else
           return respond_xml("NOT OK", "File does not exist");
}

main();

?>

 

This code works and gives a correct XML response when you type in for example "http://localhost:8080/php/script.php?file=test" it's says:

<statuscheck>
<status>NOT OK</status>
<message>File does not exist</message>
</statuscheck>

 

Now, I need it to be able to check it every 5 seconds so i've use this code

header("Refresh: 5; URL=http://localhost:8080/php/script.php?file=test");

 

Here's my problem:

 

The first 5 or 10 tries it should say "file not found".

After the 10th try it should automatically say "file found".

BUT it should also time out after like 50 tries and it hasn't found the file.

But i don't know how to do this..

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.