Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overwriting a file in Linux not working - works in Windows

Status
Not open for further replies.

csbdeady

Programmer
May 18, 2002
119
GB
Hi there

I've a PHP script that searches for a string in a text file and overwrites that string with a different string if the first string exists - standard find and replace stuff. This works fine on my windows machine test machine, but does not work on the server.

Both local and server run PHP 4.2.1.

I'm using str_replace() to replace the string.

The permissions on the server I've tried are 777, 755, 666.

Any thoughts on what I may be doing wrong?

Thanks!
-Colin
 
What errors are you getting? ______________________________________________________________________
TANSTAAFL!
 
I'm not getting any errors at all - only that the string in the file is not being overwritten.

Now, to confuse matters if I do:

strpos(text_of_file_as_a_string,string_to_search_for)

I get a positive result on the local windows machine, ie: it finds it, but I get a result of false on the Linux server.

If I do:

strpos(text_of_file_as_a_string,"EST")

I get the answer 933 on the windows machine, but 928 on the Linux... which to me suggests that the Linux machine is reading extra characters into the string which may be getting in the way when comparing long string that have such things as double-quotation marks in them.

-Colin
 
Remember, too that newlines are different on Linux and Win32 -- Win32 uses <carriage return><line feed> to denote a new line, but Linux uses only <line feed>.

That's one possibility for the discrepancy in the string position.

One thing to try. I don't know how you're attempting to write to the file, but as a test, you might try reading the original, performing your edit, then writing to a second file. ______________________________________________________________________
TANSTAAFL!
 
THanks

I've stripped out all the CR and LF but still get the problem.

I know the problem lies before the write-to-file part of the code as the string comparisons return different results on the two platforms.

*sigh* am really stuck here
-Colin
 
Oh I should say, this is the code I'm using to do the string find and replace:

$file = implode( &quot;&quot;, file( &quot;partners.txt&quot;));
$newfile=str_replace($modmem, $newname.chr(13).chr(10), $file);

My understanding is that it reads in partners.txt, implodes it into one whole so str_replace can work its stuff. Later on in the code I write it all back out again.
 
Test every step. Outout $file after the implode() to insure that it contains the text you think.

Do the same for $newfile after str_replace() ______________________________________________________________________
TANSTAAFL!
 
Thanks but they do contain what I think they should - that's the weird thing - on a windows machine it works 100%, but on a linux machine str_replace() won't work based on the code snippet above.

However if I replace:

$file = implode( &quot;&quot;, file( &quot;partners.txt&quot;));
$newfile=str_replace($modmem, $newname.chr(13).chr(10), $file);

with

$file = implode( &quot;&quot;, file( &quot;partners.txt&quot;));
$newfile=str_replace(&quot;XYZ&quot;, $newname.chr(13).chr(10), $file);

Then XYZ is replaced correctly.
If I echo $modmem to the screen I actually get:

&quot;XYZ &quot;
not &quot;XYZ&quot;

It seems that Windows doesn't have a problem with this trailing space - I've no idea where it comes from as it's not in the file (after the Z we have a CR/LF). I'll try trimming the space and seeing if that works - any other thoughts ??

Ta!
 
Trim it.

Does $modmodem have the trailing space on Win32? ______________________________________________________________________
TANSTAAFL!
 
is one uploading the file as binary or acsii ?

If you can get a command on the *nix server dtox the file once you've uploaded it. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
solved it!

T'was that rtrim() that fixed things.

it seems that passing a variable into str_replace() for some reason was as we suspected putting a blank character of some sort on the end of the line - I'm thinking the EOL character. Windows appeared to ignore this (even though it showed it as a space when echoing to the browser), but Linux got all picky;)

Actually being fair to Linux, it was correct as &quot;XYZ<EOL>&quot; is not the same as &quot;XYZ&quot;.

doing:

str_replace(rtrim(stripslahes($variable)),y,z)

appears to be the key.

All is working fine now (well until the next time...;)

Many many thanks once again for your help u wonderful ppl.

10 to 1 in the morning - time for sleep zzZz
-Colin
 
That's right, Win32 actually has an end-of-file character -- it's whatever the ASCII of Crtl-Z is. ______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top