file() doesn't work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Moore

    file() doesn't work

    Hi All,
    Ok, here's my problem. I want to open a file and process its contents.
    However, because it is possible that the file may not exist, I also want to
    check whether the file() function is successful before attempting to process
    any returned data from the file() function. The php file() manual page
    suggests that it returns FALSE if it is unsuccessful, however this simply
    doesn't happen on my system. My code looks like the following:


    if (($file_content s =
    (file("http://www.mydomain.co m/some_file_that_ doesnt_exist.da t"))) == FALSE)
    {
    echo "Your file doesn't exist!!";
    }
    else
    {
    do_some_interes ting_processing ($file_contents );
    }

    Even something like the following doesn't work:

    if (file("http://www.mydomain.co m/some_file_that_ doesnt_exist.da t") ==
    FALSE)
    {
    echo "Your file doesn't exist!!";
    return;
    }

    The file() function doesn't return FALSE as suggested.

    So what's going on?. Why doesn't this work?. Is there another/better way of
    knowing whether the file() function was successful?.

    Regards,
    Dave


  • frizzle

    #2
    Re: file() doesn't work

    Wouldn't

    if( !file('file.dat ') )
    {
    echo 'doesnt work';
    }

    work?

    Comment

    • Janwillem Borleffs

      #3
      Re: file() doesn't work

      Dave Moore wrote:[color=blue]
      > Hi All,
      > Ok, here's my problem. I want to open a file and process its
      > contents. However, because it is possible that the file may not
      > exist, I also want to check whether the file() function is successful
      > before attempting to process any returned data from the file()
      > function. The php file() manual page suggests that it returns FALSE
      > if it is unsuccessful, however this simply doesn't happen on my
      > system. My code looks like the following:
      >
      >[/color]

      It does work as is demonstrated with the following:

      print file('http://example.com/lost') == false ? 1 : 0;

      Have a look at what the file() function call actually returns (e.g., with
      print_r()). Chances are that because of some directive, your webserver does
      not return a 404 response code, but redirects with a 301/302 response code
      to some other page.


      JW


      Comment

      • Dave Moore

        #4
        Re: file() doesn't work

        Thanks. That seems to be working now.

        Ta,
        Dave

        "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
        news:43b2f5f2$0 $62369$dbd4f001 @news.euronet.n l...[color=blue]
        > Dave Moore wrote:[color=green]
        > > Hi All,
        > > Ok, here's my problem. I want to open a file and process its
        > > contents. However, because it is possible that the file may not
        > > exist, I also want to check whether the file() function is successful
        > > before attempting to process any returned data from the file()
        > > function. The php file() manual page suggests that it returns FALSE
        > > if it is unsuccessful, however this simply doesn't happen on my
        > > system. My code looks like the following:
        > >
        > >[/color]
        >
        > It does work as is demonstrated with the following:
        >
        > print file('http://example.com/lost') == false ? 1 : 0;
        >
        > Have a look at what the file() function call actually returns (e.g., with
        > print_r()). Chances are that because of some directive, your webserver[/color]
        does[color=blue]
        > not return a 404 response code, but redirects with a 301/302 response code
        > to some other page.
        >
        >
        > JW
        >
        >[/color]


        Comment

        Working...