You are here: Re: Re-opening a file « PHP Programming Language « IT news, forums, messages
Re: Re-opening a file

Posted by W Marsh on 07/15/07 17:22

On 15 Jul, 16:53, Rami Elomaa <rami.elo...@gmail.com> wrote:
> 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.

Thanks. That seems like a plausible solution. I'll try it at work
tomorrow.

I don't think I can use file_get_contents in this case, because I want
to respect file locks. It would be nice if I could get
file_get_contents to do that.

>
> 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.

It wasn't the request that annoyed me, but the sarcasm. I don't want
to derail things further by arguing about it though!

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация