Re-opening a file - PHP

This is a discussion on Re-opening a file - PHP ; Hi. In my application I do something very simple - I open a file, lock it exclusively, write some data to it and close it. If I re-open it in the same process (I mean before the script has ended) ...

+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 29

Re-opening a file

  1. Default Re-opening a file

    Hi. In my application I do something very simple - I open a file, lock
    it exclusively, write some data to it and close it.

    If I re-open it in the same process (I mean before the script has
    ended) to read the file, I find that the data I have written doesn't
    exist in there. I can't read it back until I run the script again.

    My question follows: WHAT


  2. Default Re: Re-opening a file

    W Marsh wrote:
    > Hi. In my application I do something very simple - I open a file, lock
    > it exclusively, write some data to it and close it.
    >
    > If I re-open it in the same process (I mean before the script has
    > ended) to read the file, I find that the data I have written doesn't
    > exist in there. I can't read it back until I run the script again.
    >
    > My question follows: WHAT
    >


    Sorry, my crystal ball is in the shop. I have no idea what might be
    happening. Maybe if you post your code...

    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attglobal.net
    ==================

  3. Default Re: Re-opening a file

    On 15 Jul, 14:41, Jerry Stuckle <jstuck...@attglobal.net> wrote:
    > W Marsh wrote:
    > > Hi. In my application I do something very simple - I open a file, lock
    > > it exclusively, write some data to it and close it.

    >
    > > If I re-open it in the same process (I mean before the script has
    > > ended) to read the file, I find that the data I have written doesn't
    > > exist in there. I can't read it back until I run the script again.

    >
    > > My question follows: WHAT

    >
    > Sorry, my crystal ball is in the shop. I have no idea what might be
    > happening. Maybe if you post your code...
    >
    > --
    > ==================
    > Remove the "x" from my email address
    > Jerry Stuckle
    > JDS Computer Training Corp.
    > jstuck...@attglobal.net
    > ==================


    No. For one thing it's private, commercial code, and secondly, I've
    given enough information about the problem.

    I don't appreciate people making sarcastic comments like that. If you
    have no idea about the problem then keep quiet, or if you genuinely
    are missing important details, make a polite request, e.g. "Are you
    doing X between closing and re-opening the file?". I'm not an outlet
    for problems with your insecurities and ego.

    If you really are that unimaginative, then here's some code (using my
    own original description as a design spec, proving my point somewhat):

    define(FILE_NAME, "somefile.txt");

    $dataOrig = array("here", "is", "some", "data", "to", "be",
    "serialized");

    $file = fopen(FILE_NAME, "w");
    flock($file, LOCK_EX);
    fwrite($file, serialize($dataOrig));
    fclose($file);

    $file = fopen(FILE_NAME, "r");
    $dataNew = unserialize(fread($file, filesize(FILE_NAME)));

    print_r($dataNew); // Expected output: Array ( [0] => here [1] => is
    [2] => some [3] => data [4] => to [5] => be [6] => serialized )

    It is not representative of the original code in terms of design or
    error checking, but it does reproduce the problem on the production
    server (no output, due to getting no data in the original file). It
    does however run correctly on a my own web server, which suggests the
    problem is one of configuration that somebody with experience in this
    field could shed some light on, which I would be grateful for.

    Do you see?


  4. Default Re: Re-opening a file

    "W Marsh" <wayne.marsh@gmail.com> wrote in message
    news:1184507821.293218.166830@57g2000hsv.googlegroups.com...
    > On 15 Jul, 14:41, Jerry Stuckle <jstuck...@attglobal.net> wrote:


    >> Sorry, my crystal ball is in the shop. I have no idea what might be
    >> happening. Maybe if you post your code...


    > I don't appreciate people making sarcastic comments like that. If you
    > have no idea about the problem then keep quiet, or if you genuinely
    > are missing important details, make a polite request, e.g. "Are you
    > doing X between closing and re-opening the file?". I'm not an outlet
    > for problems with your insecurities and ego.


    You are, however, the one with the broken code that is asking the question.

    --
    Richard.



  5. Default Re: Re-opening a file

    On 15 Jul, 15:08, "rf" <r...@invalid.com> wrote:
    > "W Marsh" <wayne.ma...@gmail.com> wrote in message
    >
    > news:1184507821.293218.166830@57g2000hsv.googlegroups.com...
    >
    > > On 15 Jul, 14:41, Jerry Stuckle <jstuck...@attglobal.net> wrote:
    > >> Sorry, my crystal ball is in the shop. I have no idea what might be
    > >> happening. Maybe if you post your code...

    > > I don't appreciate people making sarcastic comments like that. If you
    > > have no idea about the problem then keep quiet, or if you genuinely
    > > are missing important details, make a polite request, e.g. "Are you
    > > doing X between closing and re-opening the file?". I'm not an outlet
    > > for problems with your insecurities and ego.

    >
    > You are, however, the one with the broken code that is asking the question.
    >
    > --
    > Richard.


    Ah, so you've seen where the code I've posted is broken. Excellent! I
    am anxious to see you elaborate further on this.


  6. Default Re: Re-opening a file

    W Marsh wrote:
    > On 15 Jul, 14:41, Jerry Stuckle <jstuck...@attglobal.net> wrote:
    >> Sorry, my crystal ball is in the shop. I have no idea what might be
    >> happening. Maybe if you post your code...
    >>
    >> --
    >> ==================
    >> Remove the "x" from my email address
    >> Jerry Stuckle
    >> JDS Computer Training Corp.
    >> jstuck...@attglobal.net
    >> ==================

    >
    > No. For one thing it's private, commercial code, and secondly, I've
    > given enough information about the problem.
    >


    Maybe in your opinion you have. But you have given us no idea about how
    you're going about the problem or a lot of other things.

    If you had given enough information, we could help you.

    And BTW - in PHP there is no such thing as "private" code. Once you
    give it to a customer the source is available, unless you get one of the
    encryption engines, i.e. from Zend.

    > I don't appreciate people making sarcastic comments like that. If you
    > have no idea about the problem then keep quiet, or if you genuinely
    > are missing important details, make a polite request, e.g. "Are you
    > doing X between closing and re-opening the file?". I'm not an outlet
    > for problems with your insecurities and ego.
    >


    And I don't appreciate people arguing that they have provided enough
    information when they are asking for help.

    I have no insecurities, and my ego isn't a problem. But obviously yours
    are.

    > If you really are that unimaginative, then here's some code (using my
    > own original description as a design spec, proving my point somewhat):
    >


    I don't need to be "imaginative". I can come up with at least a half
    dozen ways to do what your description says you're doing.

    I need to see the code YOU are using.

    If you want help here, you need to get rid of the attitude and post
    something which SHOWS the problem.

    > define(FILE_NAME, "somefile.txt");
    >
    > $dataOrig = array("here", "is", "some", "data", "to", "be",
    > "serialized");
    >
    > $file = fopen(FILE_NAME, "w");
    > flock($file, LOCK_EX);
    > fwrite($file, serialize($dataOrig));
    > fclose($file);
    >
    > $file = fopen(FILE_NAME, "r");
    > $dataNew = unserialize(fread($file, filesize(FILE_NAME)));
    >
    > print_r($dataNew); // Expected output: Array ( [0] => here [1] => is
    > [2] => some [3] => data [4] => to [5] => be [6] => serialized )
    >
    > It is not representative of the original code in terms of design or
    > error checking, but it does reproduce the problem on the production
    > server (no output, due to getting no data in the original file). It
    > does however run correctly on a my own web server, which suggests the
    > problem is one of configuration that somebody with experience in this
    > field could shed some light on, which I would be grateful for.
    >
    > Do you see?
    >


    Now we're getting someplace - and this is MUCH DIFFERENT than your
    original problem. The fact it works on one server but not another is
    something you never mentioned before, for instance.

    For one thing, you don't have all errors enabled - or you would have
    seen that your first line is wrong. It should be:

    define('FILE_NAME', "somefile.txt");

    Now some other questions:

    What versions of PHP are the two systems using?
    What OS are they using?
    What directory are you writing the file to?
    Do you have permission to flock() the file?

    Once I corrected the above problem your code works on two different systems.

    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attglobal.net
    ==================

  7. Default Re: Re-opening a file

    On 15 Jul, 15:27, Jerry Stuckle <jstuck...@attglobal.net> wrote:
    > W Marsh wrote:
    > > On 15 Jul, 14:41, Jerry Stuckle <jstuck...@attglobal.net> wrote:
    > >> Sorry, my crystal ball is in the shop. I have no idea what might be
    > >> happening. Maybe if you post your code...

    >
    > > No. For one thing it's private, commercial code, and secondly, I've
    > > given enough information about the problem.

    >
    > Maybe in your opinion you have. But you have given us no idea about how
    > you're going about the problem or a lot of other things.
    >
    > If you had given enough information, we could help you.
    >
    > And BTW - in PHP there is no such thing as "private" code. Once you
    > give it to a customer the source is available, unless you get one of the
    > encryption engines, i.e. from Zend.


    Well, there is. Not a single person outside of the company will ever
    see this code, because it's a backend system that powers customer
    facing front ends. Surely you have come across such a situation
    before! Not all PHP ends up as forum software.

    >
    > > I don't appreciate people making sarcastic comments like that. If you
    > > have no idea about the problem then keep quiet, or if you genuinely
    > > are missing important details, make a polite request, e.g. "Are you
    > > doing X between closing and re-opening the file?". I'm not an outlet
    > > for problems with your insecurities and ego.

    >
    > And I don't appreciate people arguing that they have provided enough
    > information when they are asking for help.
    >
    > I have no insecurities, and my ego isn't a problem. But obviously yours
    > are.


    I'm sure you don't like Google Groups, but it's a useful tool.
    Searching your posts reveals the same attitude across the board. Your
    prey is innocent people requesting help, and you feed on them by
    taking any opportunity you can to point out flaws in their questions,
    or failing that, their posting style. It's an all too common geekish
    attitude, and you're only embarrassing yourself by conforming
    wholesale to an unattractive, unintelligent stereotype. Pretending
    that you don't understand the question because it isn't formatted to
    your requirements is just obtuse. I understand your pain, and I'm
    guilty of similar things to some extent, but your arrogance record and
    tag team bullying is awful.

    >
    > > If you really are that unimaginative, then here's some code (using my
    > > own original description as a design spec, proving my point somewhat):

    >
    > I don't need to be "imaginative". I can come up with at least a half
    > dozen ways to do what your description says you're doing.
    >
    > I need to see the code YOU are using.
    >
    > If you want help here, you need to get rid of the attitude and post
    > something which SHOWS the problem.


    If you want to help here... etc. etc.

    >
    >
    >
    > > define(FILE_NAME, "somefile.txt");

    >
    > > $dataOrig = array("here", "is", "some", "data", "to", "be",
    > > "serialized");

    >
    > > $file = fopen(FILE_NAME, "w");
    > > flock($file, LOCK_EX);
    > > fwrite($file, serialize($dataOrig));
    > > fclose($file);

    >
    > > $file = fopen(FILE_NAME, "r");
    > > $dataNew = unserialize(fread($file, filesize(FILE_NAME)));

    >
    > > print_r($dataNew); // Expected output: Array ( [0] => here [1] => is
    > > [2] => some [3] => data [4] => to [5] => be [6] => serialized )

    >
    > > It is not representative of the original code in terms of design or
    > > error checking, but it does reproduce the problem on the production
    > > server (no output, due to getting no data in the original file). It
    > > does however run correctly on a my own web server, which suggests the
    > > problem is one of configuration that somebody with experience in this
    > > field could shed some light on, which I would be grateful for.

    >
    > > Do you see?

    >
    > Now we're getting someplace - and this is MUCH DIFFERENT than your
    > original problem. The fact it works on one server but not another is
    > something you never mentioned before, for instance.


    Yes, that would have been useful information. I would have provided it
    in a much nicer way if you made a less sarcastic request, and I hadn't
    just looked at your other posts!

    >
    > For one thing, you don't have all errors enabled - or you would have
    > seen that your first line is wrong. It should be:
    >
    > define('FILE_NAME', "somefile.txt");


    Yuck. I dislike PHP, as a language. I wish that I didn't have to use
    it. It seems almost designed to catch the programmer out - a syntax
    error should be rigid. The fact that the code works anyway seems more
    like a "syntax recommendation". It is ridiculous for PHP to make the
    assumption that the programmer has read every detail of the (poor)
    documentation that exists for language elements, and punish the time-
    pressed newcomer by expecting him to know that there is an option for
    enabling all errors.

    > Now some other questions:
    >
    > What versions of PHP are the two systems using?


    I can't be minor version accurate about the production server on a
    Sunday, but I know it's PHP5. The server I did the successful test on
    is running PHP 4.4.2.

    > What OS are they using?


    Production: Gentoo Linux.
    Test: Ubuntu Linux

    > What directory are you writing the file to?


    Production: /usr/censored/censored2/files/
    Test: /home/var/wwwroot/vhosts/sitename.com/httpdocs

    > Do you have permission to flock() the file?


    Yes.

    > Once I corrected the above problem your code works on two different systems.


    I suspect it will work on most systems, which is unfortunate for me,
    as it probably means I am facing an obscure configuration issue.

    Thanks for your help.



  8. Default Re: Re-opening a file

    W Marsh kirjoitti:
    > On 15 Jul, 14:41, Jerry Stuckle <jstuck...@attglobal.net> wrote:
    >> W Marsh wrote:
    >>> Hi. In my application I do something very simple - I open a file, lock
    >>> it exclusively, write some data to it and close it.
    >>> If I re-open it in the same process (I mean before the script has
    >>> ended) to read the file, I find that the data I have written doesn't
    >>> exist in there. I can't read it back until I run the script again.
    >>> My question follows: WHAT

    >> Sorry, my crystal ball is in the shop. I have no idea what might be
    >> happening. Maybe if you post your code...
    >>
    >> --
    >> ==================
    >> Remove the "x" from my email address
    >> Jerry Stuckle
    >> JDS Computer Training Corp.
    >> jstuck...@attglobal.net
    >> ==================

    >
    > No. For one thing it's private, commercial code, and secondly, I've
    > given enough information about the problem.
    >
    > I don't appreciate people making sarcastic comments like that. If you
    > have no idea about the problem then keep quiet, or if you genuinely
    > are missing important details, make a polite request, e.g. "Are you
    > doing X between closing and re-opening the file?". I'm not an outlet
    > for problems with your insecurities and ego.
    >
    > If you really are that unimaginative, then here's some code (using my
    > own original description as a design spec, proving my point somewhat):
    >
    > define(FILE_NAME, "somefile.txt");
    >
    > $dataOrig = array("here", "is", "some", "data", "to", "be",
    > "serialized");
    >
    > $file = fopen(FILE_NAME, "w");
    > flock($file, LOCK_EX);
    > fwrite($file, serialize($dataOrig));
    > fclose($file);
    >
    > $file = fopen(FILE_NAME, "r");
    > $dataNew = unserialize(fread($file, filesize(FILE_NAME)));
    >
    > print_r($dataNew); // Expected output: Array ( [0] => here [1] => is
    > [2] => some [3] => data [4] => to [5] => be [6] => serialized )
    >
    > It is not representative of the original code in terms of design or
    > error checking, but it does reproduce the problem on the production
    > server (no output, due to getting no data in the original file). It
    > does however run correctly on a my own web server, which suggests the
    > problem is one of configuration that somebody with experience in this
    > field could shed some light on, which I would be grateful for.


    I'm not sure if you already mentioned this, but if you manuallu open the
    file after running the script, are the values actually written into it,
    on the server where it doesn't work? I mean have you really checked that
    the problem is not in writing to it, but actually reading from it?

    What does
    var_dump( filesize(FILE_NAME) );
    give you?
    Try putting it before the line
    $dataNew = unserialize(fread($file, filesize(FILE_NAME)));

    If it yields zero, which might be possible for just created file it
    might be getting it from cache. If this is the case, then running
    clearstatcache() ought to solve the problem.

    BTW, I didn't think Jerry's request for code was unreasonable. For
    instance there are many ways for reading from a file. I for one couldn't
    have imagined using fread($file, filesize(FILE_NAME)) for reading, cos I
    always use file_get_contents. That's why I think it was good that you
    provided the sample code rather than leave us scraching our heads with
    nothing. When someone asks you for sample code, of course you don't need
    to post the in-house production code, just something that effectively
    reproduces the error.

    --
    Rami.Elomaa@gmail.com

    "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
    usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

  9. Default Re: Re-opening a file

    On Jul 15, 10:52 am, W Marsh <wayne.ma...@gmail.com> wrote:
    >
    > Yuck. I dislike PHP, as a language. I wish that I didn't have to use
    > it. It seems almost designed to catch the programmer out - a syntax
    > error should be rigid. The fact that the code works anyway seems more
    > like a "syntax recommendation". It is ridiculous for PHP to make the
    > assumption that the programmer has read every detail of the (poor)
    > documentation that exists for language elements, and punish the time-
    > pressed newcomer by expecting him to know that there is an option for
    > enabling all errors.
    >


    That's because you, like a lot of other people who have problems but
    can't figure out why, have error_reporting turned off. In a
    development environment it should be set to E_ALL with
    display_errors() turned on. In a production environment it should be
    set to E_ALL with display_errors() turned off and log_errors() turned
    on.

    Way to many people don't see such problems because error reporting is
    disabled. The problem is further compounded by the people who suggest
    that you can "fix" the issue of notices and warnings appearing by
    simply disabling it. Just because you can't see it doesn't mean it
    isn't there.


  10. Default Re: Re-opening a file

    On 15 Jul, 18:04, ZeldorBlat <zeldorb...@gmail.com> wrote:
    > On Jul 15, 10:52 am, W Marsh <wayne.ma...@gmail.com> wrote:
    >
    >
    >
    > > Yuck. I dislike PHP, as a language. I wish that I didn't have to use
    > > it. It seems almost designed to catch the programmer out - a syntax
    > > error should be rigid. The fact that the code works anyway seems more
    > > like a "syntax recommendation". It is ridiculous for PHP to make the
    > > assumption that the programmer has read every detail of the (poor)
    > > documentation that exists for language elements, and punish the time-
    > > pressed newcomer by expecting him to know that there is an option for
    > > enabling all errors.

    >
    > That's because you, like a lot of other people who have problems but
    > can't figure out why, have error_reporting turned off. In a
    > development environment it should be set to E_ALL with
    > display_errors() turned on. In a production environment it should be
    > set to E_ALL with display_errors() turned off and log_errors() turned
    > on.
    >
    > Way to many people don't see such problems because error reporting is
    > disabled. The problem is further compounded by the people who suggest
    > that you can "fix" the issue of notices and warnings appearing by
    > simply disabling it. Just because you can't see it doesn't mean it
    > isn't there.


    Re-read what I wrote:

    "It is ridiculous for PHP to make the assumption that the programmer
    has read every detail of the (poor)documentation that exists for
    language elements, and *punish the time-pressed newcomer by expecting
    him to know that there is an option for enabling all errors.*"


+ Reply to Thread
Page 1 of 3 1 2 3 LastLast

Similar Threads

  1. opening a file
    By Application Development in forum C
    Replies: 6
    Last Post: 07-23-2007, 05:14 PM
  2. Replies: 9
    Last Post: 02-24-2007, 02:15 PM
  3. hELP OPENING FILE
    By Application Development in forum Graphics
    Replies: 9
    Last Post: 08-13-2006, 06:29 PM
  4. Replies: 0
    Last Post: 01-26-2006, 09:56 AM
  5. Re: Opening a file
    By Application Development in forum Clarion
    Replies: 0
    Last Post: 09-23-2005, 01:47 PM