Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    Seattle, WA USA
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Little problem with a script. (Getting info from a server with fopen) - Please help!

    Ok, I have this tiny script, which will basically check to see if a file exists on my server - if it does exist, it returns with an "Online" if it dosnt (Or if it cannot find the file actually) It returns with an "Offline" ... this is so, on my webpage, I can let people know when my server is online or offline - automatically.

    The script worked fine, then, suddenly, it stopped working ... I really don't think I have modified it at all.

    (I have it in a little iframe on my main page)

    So, this is the page with the script:


    <html>
    <head>
    <style>
    body {background:black;colorrange}
    #Off {color:red;text-align:center}
    #On {color:lime;text-align:center}
    </style>
    </head>
    <body topmargin="0" leftmargin="0">
    <center>
    <h2>
    <?php

    $file = @fopen("ftp://testserver:servertest@myserver.address.org/test.html", "r");
    if (!$file) {
    echo '<b id=Off>&nbsp; &nbsp; Offline</b>';
    exit;
    }else{
    echo '<b id=On>&nbsp; &nbsp; &nbsp;Online</b>';
    }
    ?>
    </body>
    </html>

    It DOES return an "Online" when the server is online -- it just dosnt return the Offline when the server is offline.

    Any help greatly appreciated!!!

    (btw, originally, I had it as
    $file = fopen
    but, it returned a text error saying that test.html could not be found or something, so, I was told to put the @ efore the fopen so it wouldnt return any error)

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    A much easier way
    PHP Code:
    if(file_exists("ftp://yoursite.com/file.txt")){
      echo 
    'Server online';
    }else{
      echo 
    'Server offline';


  • #3
    New Coder
    Join Date
    Jun 2002
    Location
    Seattle, WA USA
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    Thank you Nightfire for your response, but it didnt seem to work...

    This is what I have:

    <html>
    <body topmargin="0" leftmargin="0">
    <center>
    <h2>
    <?php

    if(file_exists("ftp://testuser:testpass@myserver.dyndns.org/test.txt")){
    echo 'Server online';
    }else{
    echo 'Server offline';
    }

    ?>
    </h2>
    </body>
    </html>


    It now dosnt ever seem to check --- nothing appears on my server's logs.
    It simply promptly responds with Server Offline ...
    I created test.txt and everything.
    Anyway, have any ideas? Thanks!

  • #4
    New Coder
    Join Date
    Jun 2002
    Location
    Seattle, WA USA
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    *bump*

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    for an FTP server you are better off trying the FTP functions ?
    PHP Code:
    <?php 
    $server
    ='ftp.your_domain.com';
    $user='username';
    $pass='password';
    $con = @ftp_connect($server,21); 
    $login = @ftp_login($con$user$pass); 
    if ((!
    $con) || (!$login)) { 
           echo 
    "FTP offline or bad user/pass"
            
    ftp_close($con); 
       } else { 
           echo 
    "FTP online"
       } 
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    New Coder
    Join Date
    Jun 2002
    Location
    Seattle, WA USA
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much! It works fast and lovely.
    I really appreciate it!

    It said warning blah blah blah when the server was offline, so I just added a litle @ sign before the ftp_close($con); and it fixed it great =)

    THANKS!

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    DOH! , actually I got the ftp_close in the wrong place ~

    glad it helped though .

    PHP Code:
    <?php 
    $server
    ='ftp.your_domain.com';
    $user='username';
    $pass='password';
    $con = @ftp_connect($server,21); 
    $login = @ftp_login($con$user$pass); 
    if ((!
    $con) || (!$login)) { 
           echo 
    "FTP offline or bad user/pass";  
       } else { 
           echo 
    "FTP online"
           
    ftp_close($con);
       } 
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •