By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,679 Members | 1,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,679 IT Pros & Developers. It's quick & easy.

Re-opening a file

P: n/a
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

Jul 15 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
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.
js*******@attglobal.net
==================
Jul 15 '07 #2

P: n/a
On 15 Jul, 14:41, Jerry Stuckle <jstuck...@attglobal.netwrote:
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?

Jul 15 '07 #3

P: n/a
rf
"W Marsh" <wa*********@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
On 15 Jul, 14:41, Jerry Stuckle <jstuck...@attglobal.netwrote:
>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.
Jul 15 '07 #4

P: n/a
On 15 Jul, 15:08, "rf" <r...@invalid.comwrote:
"W Marsh" <wayne.ma...@gmail.comwrote in message

news:11**********************@57g2000hsv.googlegro ups.com...
On 15 Jul, 14:41, Jerry Stuckle <jstuck...@attglobal.netwrote:
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.

Jul 15 '07 #5

P: n/a
W Marsh kirjoitti:
On 15 Jul, 14:41, Jerry Stuckle <jstuck...@attglobal.netwrote:
>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.

--
Ra*********@gmail.com

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

P: n/a
On 15 Jul, 16:53, Rami Elomaa <rami.elo...@gmail.comwrote:
W Marsh kirjoitti:
On 15 Jul, 14:41, Jerry Stuckle <jstuck...@attglobal.netwrote:
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!

Jul 15 '07 #7

P: n/a
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:r8******************************@comcast.com. ..
>W Marsh wrote:
>On 15 Jul, 15:27, Jerry Stuckle <jstuck...@attglobal.netwrote:
>>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.

So? That makes it so special? ROFLMAO!
That makes it _private_. You claimed that no php code is private, because
the client will always see it. Yet, if the case happens to be that a company
has it's own IT department that produces software, it will remain private,
since no second party will have access to it. You ought to be laughing at
yourself, not to his claim.

--
Ra*********@gmail.com

"Good tea. Nice house." -- Worf
Jul 16 '07 #8

P: n/a
Rami Elomaa wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:r8******************************@comcast.com. ..
>W Marsh wrote:
>>On 15 Jul, 15:27, Jerry Stuckle <jstuck...@attglobal.netwrote:
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.
So? That makes it so special? ROFLMAO!

That makes it _private_. You claimed that no php code is private, because
the client will always see it. Yet, if the case happens to be that a company
has it's own IT department that produces software, it will remain private,
since no second party will have access to it. You ought to be laughing at
yourself, not to his claim.
Nope, that doesn't make it private. It just means it has restricted
access. Different things.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 16 '07 #9

P: n/a
W Marsh wrote:

Oh, and BTW - I didn't even know that you were posting from Google
Groups until you mentioned it. That's when I looked at the headers and
verified it.

And it explains your postings completely.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 16 '07 #10

P: n/a
C.
On 15 Jul, 14:57, W Marsh <wayne.ma...@gmail.comwrote:
On 15 Jul, 14:41, Jerry Stuckle <jstuck...@attglobal.netwrote:
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?

AIR filestats are cached therefore filesize() reports the same value
after you've changed a file. RTFM for clearstatcache.

C.

Jul 16 '07 #11

P: n/a
On Sun, 15 Jul 2007 05:43:20 -0700, W Marsh <wa*********@gmail.com>
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
It works for me okay. Would you post you're code to see what's the
problem?
--
Brendan Gillatt
www.brendangillatt.co.uk
GPG: 0x6E265E61
Jul 16 '07 #12

P: n/a
Jerry Stuckle kirjoitti:
Rami Elomaa wrote:
>"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:r8******************************@comcast.com ...
>>W Marsh wrote:
On 15 Jul, 15:27, Jerry Stuckle <jstuck...@attglobal.netwrote:
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.

So? That makes it so special? ROFLMAO!

That makes it _private_. You claimed that no php code is private,
because the client will always see it. Yet, if the case happens to be
that a company has it's own IT department that produces software, it
will remain private, since no second party will have access to it. You
ought to be laughing at yourself, not to his claim.

Nope, that doesn't make it private. It just means it has restricted
access. Different things.
Whatever you wanna call it, same difference.

--
Ra*********@gmail.com

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

P: n/a
On 16 Jul, 12:10, Jerry Stuckle <jstuck...@attglobal.netwrote:
Rami Elomaa wrote:
"Jerry Stuckle" <jstuck...@attglobal.netwrote in message
news:r8******************************@comcast.com. ..
W Marsh wrote:
On 15 Jul, 15:27, Jerry Stuckle <jstuck...@attglobal.netwrote:
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.
So? That makes it so special? ROFLMAO!
That makes it _private_. You claimed that no php code is private, because
the client will always see it. Yet, if the case happens to be that a company
has it's own IT department that produces software, it will remain private,
since no second party will have access to it. You ought to be laughing at
yourself, not to his claim.

Nope, that doesn't make it private. It just means it has restricted
access. Different things.
Wheeeeee.

That's the sound of you moving the goalposts.

Jul 17 '07 #14

P: n/a
W Marsh wrote:
>Nope, that doesn't make it private. It just means it has restricted
access. Different things.

Wheeeeee.

That's the sound of you moving the goalposts.
Go play with your code, troll. You obviously don't understand what
"private code" is any more than you understand how to ask questions in a
newsgroup.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 18 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.