Mysterious Script Stops When Getting Files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric Walker

    Mysterious Script Stops When Getting Files

    I am a php beginner, so apologize if this question is silly.

    I am encountering apparently random script stops when I try to
    get a remote HTML file with either file() or fopen(). A
    typical code snippet:

    echo 'here we go...';
    $handle=fopen($ url,'r');
    echo 'and here we are';
    if ($handle==FALSE )
    {
    do some stuff
    }

    I can try to use that to obtain several hundred similar files,
    and have it go through a dozen or a hundred of them getting,
    every time,

    here we go...and here we are

    and, more important, the file read just fine. But, sooner
    or later in such a run, I will get the first echoed message but
    not the second (that is, "here we go..." and nothing else) and
    the script will have stopped dead. I have this in the script:

    function bye()
    {
    echo 'SHUT DOWN!'.$p;
    }
    register_shutdo wn_function('by e');

    I never see that message when it stops (though i do if the
    script hits an exit statement). I also have this:

    set_time_limit( 0);

    The php version is 4.2.2 on a server running BSD.

    I encounter exactly the same behavior trying to fetch files
    using

    $remotefile=fil e($url);

    On random occasions it just never comes out of the function.

    This is driving me mad. Can anyone tell me what I am doing
    wrong?


    --
    Cordially,
    Eric Walker
    Owlcroft House


  • Eric Walker

    #2
    Re: Mysterious Script Stops When Getting Files

    On 02 Sep 2003 10:27:37 GMT, Peter Jones wrote:
    [color=blue]
    >"Eric Walker" <ewalker@owlcro ft.com> wrote in
    >news:rjnyxrebj ypebsgpbz.hkk0c j1.pminews@news .individual.net :
    >[color=green]
    >> I am a php beginner, so apologize if this question is silly.
    >>
    >> I am encountering apparently random script stops when I try
    >> to get a remote HTML file with either file() or fopen(). A
    >> typical code snippet:
    >>
    >> echo 'here we go...';
    >> $handle=fopen($ url,'r');
    >> echo 'and here we are';
    >> if ($handle==FALSE )
    >> {
    >> do some stuff
    >> }[/color]
    >
    > else fclose($handle) ;
    >
    >I would imagine you are hitting the system limit on
    >concurrently open files, if there isn't an fclose() somewhere
    >closing them again... Adding the above line ought to fix
    >that.[/color]

    Sorry, no, it isn't that. For simplicity's sake, I just showed
    the immediately relevant lines in the snippet; there is indeed
    a line--

    if ($handle!==FALS E) fclose $handle;

    farther on.

    [color=blue]
    >PS: Is that logic correct? 'do some stuff' if file did NOT
    >open?[/color]

    Some error-recording stuff, yes, and re-try logic; there is
    also continuing script that proceeds if the handle is valid
    (and which eventually gets to that file-close). But the script
    never gets to use anything in that block because it never
    emerges from the file-open (or, with file(), file-get) function
    call.

    What especially puzzles me is that the script apparently
    doesn't even activate the registered shutdown function when it
    quits: it just . . . stops.


    --
    Cordially,
    Eric Walker
    My opinions on English are available at
    Discussions about the English language and its right use.




    Comment

    • Krisztian VASAS

      #3
      Re: Mysterious Script Stops When Getting Files

      Eric Walker wrote:
      [color=blue]
      > On 02 Sep 2003 10:27:37 GMT, Peter Jones wrote:[color=green]
      >>"Eric Walker" <ewalker@owlcro ft.com> wrote in
      >>news:rjnyxreb jypebsgpbz.hkk0 cj1.pminews@new s.individual.ne t:
      >>
      >>[color=darkred]
      >>>I am a php beginner, so apologize if this question is silly.
      >>>
      >>>I am encountering apparently random script stops when I try
      >>>to get a remote HTML file with either file() or fopen(). A
      >>>typical code snippet:
      >>>
      >>> echo 'here we go...';
      >>> $handle=fopen($ url,'r');
      >>> echo 'and here we are';
      >>> if ($handle==FALSE )
      >>> {
      >>> do some stuff
      >>> }[/color]
      >>
      >> else fclose($handle) ;
      >>
      >>I would imagine you are hitting the system limit on
      >>concurrentl y open files, if there isn't an fclose() somewhere
      >>closing them again... Adding the above line ought to fix
      >>that.[/color]
      >
      >
      > Sorry, no, it isn't that. For simplicity's sake, I just showed
      > the immediately relevant lines in the snippet; there is indeed
      > a line--
      >
      > if ($handle!==FALS E) fclose $handle;
      >
      > farther on.[/color]

      Dummy question: see the allow_url_fopen in php.ini whether it is
      enabled. If not, then enable it...
      [color=blue][color=green]
      >>PS: Is that logic correct? 'do some stuff' if file did NOT
      >>open?[/color]
      >
      >
      > Some error-recording stuff, yes, and re-try logic; there is
      > also continuing script that proceeds if the handle is valid
      > (and which eventually gets to that file-close). But the script
      > never gets to use anything in that block because it never
      > emerges from the file-open (or, with file(), file-get) function
      > call.
      >
      > What especially puzzles me is that the script apparently
      > doesn't even activate the registered shutdown function when it
      > quits: it just . . . stops.[/color]

      [color=blue]
      > if ($handle!==FALS E) fclose $handle;[/color]

      or:

      if ($handle!=FALSE ) fclose $handle;


      IroNiQ
      --
      Web: http://ironiq.hu/
      Email: iron@ironiq.hu

      Comment

      • Mark Hewitt

        #4
        Re: Mysterious Script Stops When Getting Files



        "Peter Jones" <jonespr@optush ome.com.au> wrote in message
        news:Xns93EBEE8 F5DAE4prjpp1516 202119@210.49.2 0.254...[color=blue]
        > "Eric Walker" <ewalker@owlcro ft.com> wrote in
        > news:rjnyxrebjy pebsgpbz.hkli9d 0.pminews@news. individual.net:
        >[color=green]
        > > On 02 Sep 2003 10:27:37 GMT, Peter Jones wrote:[/color][/color]

        [snip]
        [color=blue][color=green]
        > > Sorry, no, it isn't that. For simplicity's sake, I just showed
        > > the immediately relevant lines in the snippet; there is indeed
        > > a line--
        > >
        > > if ($handle!==FALS E) fclose $handle;
        > >
        > > farther on.[/color][/color]

        [snip]
        [color=blue]
        > (Also, as Krisztian pointed out, it should probably be "!=" rather than
        > "!==", although simple testing on my part seems to suggest "!==" is
        > working the same way as "!=". According to the precedence rules it should
        > be parsing as "((!)==)" -- but actual usage of such a construct may well
        > be undefined and therefore not do what you think it will...)
        >[/color]

        No, !== is quite different to !=, as is === to ==
        Look at Comparison operators in PHP manual

        $x = 0;
        $y = false;

        if ( $x === false ) { // this is not true, x is not FALSE, its 0 }
        if ( $y === false ) { // this is true }
        if ( !$x ) { // this is true, 0 is classed as a logical false, but it does
        not have the _type_ false, it is type integer }

        Similary for !== ....
        if ( $x !== false ) { // true, it is type interger, no false, despite the
        fact that.... if ( !$x ) would be true
        if ( $y !== false ) { // false, y does have type false ... }


        I mut say here I do tend to test for :

        if ( $handle ) { fclose($handle) ; }

        myself, it reads easier, but i guess...

        if ( $handle !== false ) { ... }

        is more strictly correct, becuase fopen returns false on failure, so
        if ( $handle === false ) { it failed... }
        conversly
        if ( $handle !== false ) { it worked }

        This means should file handles suddenly start allowing 0 as a file handle it
        would still work.

        Thanks
        Mark
        ---------------------------------------------------------------------------
        Windows, Linux and Internet Development Consultant
        Email: corporate@scrip tsmiths.com
        Web: http://www.scriptsmiths.com
        ---------------------------------------------------------------------------


        [snip]

        [color=blue]
        >
        > HTH,
        > Pete.[/color]


        Comment

        • Eric Walker

          #5
          Re: Mysterious Script Stops When Getting Files

          On 03 Sep 2003 13:27:06 GMT, Peter Jones wrote:
          [color=blue]
          >"Eric Walker" <ewalker@owlcro ft.com> wrote in
          >news:rjnyxrebj ypebsgpbz.hkli9 d0.pminews@news .individual.net :
          >[color=green]
          >> On 02 Sep 2003 10:27:37 GMT, Peter Jones wrote:[/color]
          >[color=green][color=darkred]
          >>>I would imagine you are hitting the system limit on
          >>>concurrent ly open files, if there isn't an fclose()
          >>>somewhere closing them again...[/color]
          >>
          >> Sorry, no, it isn't that. For simplicity's sake, I just
          >> showed the immediately relevant lines in the snippet; there
          >> is indeed a line--
          >>
          >> if ($handle!==FALS E) fclose $handle;
          >>
          >> farther on.[/color]
          >
          >Are you sure that the script is actually reaching (and
          >correctly processing) the fclose()? Also, are you sure you
          >are not reassigning the value of $handle somewhere before you
          >do the fclose($handle) -- something I've been caught by a time
          >or two in the past...[/color]

          I am sure. Moreover, the problem originally arose using
          file(), which does not involve an explicit open/close process
          using a handle; I only went back to fopen/fclose to see if that
          would work any better (it didn't).

          [...]
          [color=blue][color=green]
          >> What especially puzzles me is that the script apparently
          >> doesn't even activate the registered shutdown function when
          >> it quits: it just . . . stops.[/color]
          >
          >The only other thing I can think to suggest is to specifically
          >check which file it is stopping on. Is it the same file each
          >time? If so, is there something else accessing that file at
          >the same time?[/color]

          No, not the same file each time: the "problem" file will be one
          that, on the next run, will be picked up just fine (and
          probably was on an earlier run)--it doesn't seem to be
          associated with the file being sought (though it may reflect
          something dicey in the server's behavior or returned headers--I
          haven't tried to look at those). Incidentally, the problem
          arise both with files off remote servers (such as Amazon and
          ESPN) and with php scripts of mine on my ISP's own server.

          Maddening!


          --
          Cordially,
          Eric Walker
          My opinions on English are available at
          Discussions about the English language and its right use.




          Comment

          • Peter Jones

            #6
            Re: Mysterious Script Stops When Getting Files

            "Mark Hewitt" <corporate@scri ptsmiths.com> wrote in
            news:3f55f747$0 $64724@hades.is .co.za:
            [color=blue]
            > No, !== is quite different to !=, as is === to ==
            > Look at Comparison operators in PHP manual[/color]

            Thanks, Mark, for elaborating on that. I did actually look in my near-to-
            hand reference for "!==" before I allowed myself to post under the premise
            that it was invalid. My reference ('PHP and MySQL Web Development') mentions
            "===" but not "!==". I guess I've learned two things -- one being that I
            can't necessarily totally trust this book now... :-)

            I guess "!==" is to "===" as "!=" is to "=="?!

            Apologies all round for taking the discussion down a false trail.

            Pete.

            Comment

            • CC Zona

              #7
              Re: Mysterious Script Stops When Getting Files

              In article <Xns93EC757FFA5 D9prjpp15162021 19@210.49.20.25 4>,
              Peter Jones <jonespr@optush ome.com.au> wrote:
              [color=blue]
              > I guess "!==" is to "===" as "!=" is to "=="?![/color]

              Correct. The former set evaluates value and type, while the latter set
              evaluates only value. So, for instance:

              1==TRUE //because their values evaluate as equivalent

              -but-

              1!==TRUE //because a boolean is not an integer

              --
              CC

              Comment

              Working...