0

My php script needs to write text file, (actualy write php code), in order to include it on the next run. So I have all my data (arrays) from the previus run, with out the use of database. The files are saved on a network drive (SAN).

The script runs 3-4 times in a sec (approx every 300 milisecs) and the size of file is 1.2 MB (approx)

Some times the file are corrupted, with missing END OF FILE (EOF)

Some times I get junks of text (repetead part of string)

eg.

$write_string = '$arrMK[1][1]=1.80;$arrMK[1][2]=1.82;$arrMK[1][3]=2.14;$arrMK[1][4]=1.80;$arrMK[2][1]=2.43;$arrMK[2][2]=1.13;$arrMK[2][3]=1.33;$arrMK[2][4]=4.11;... and so on...';

and the content of file is like (bold is repeted part):

$arrMK[1][<strong>1]=1.80;$arrMK[1][2]</strong>=1.82;$arrMK[1][3]=2.14;$arrMK[1][4]=1.80;$arrMK[2][1]=2.43;$arrMK[2][2]=1.13;$ar<strong>1]=1.80;$arrMK[1][2]</strong>rMK[2][3]=1.33;$arrMK[2][4]=4.11;

If the file size is less than 1 MB, everything is OK.

I have try using fopen/fwrite, file_put_contents, with the same results. Any suggestion about that?

Elias

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • You are not rewriting on the same output file each time. Is it? In this case you may and up reading the file while another PHP process is writing on it. If this is the case, you may want to use a locking mechanism (filesystem lock like flock() if it's supported by your filesystem, or write your own application locking scheme) – dAm2K Jun 10 '13 at 14:03
  • Nop. I write a new one each time, and delete the olds. – Elias Giannopoulos Jun 10 '13 at 19:39
  • The file system is GFS2 (global file system II) that shares the disk space between cluster-node linux setup. – Elias Giannopoulos Jun 10 '13 at 19:52

1 Answers1

0

May be the PHP script cannot complete its write operation before your user requires it. Try to call fflush() and fclose() on your script to be sure that your file is closed correctly.

Remember to pay attention to PHP timeout/tuning issues that can prevent PHP to save the file correctly. Double remember to remove old files too, or your sysadmin will kill you sooner or later...

BTW, IMHO it's a very ugly way of doing. You can use sessions to save user states and environments (check for session_start)

dAm2K
  • 9,923
  • 5
  • 44
  • 47
  • The script is called (run) from external software. So no client (web browser) for sessions. Each call (hit script) is a diff session. Also the exported data (as php code) used from several scripts (include). I don't have problems with script timeout, because the script runs for 70-80 milliseconds. Be sure im use the fclose. I will try to deal with fflush. Thanks for your time – Elias Giannopoulos Jun 10 '13 at 19:51