Write data to file, pass file to algorithm, output to array...<

liunx

Guest
Hi there,

I was wondering if anyone has any experience using PHP with a web form to write to a text file, and then pass that file through an algorithm, then get the results back and show them online.

My specific issue is this: I have to collect form variables, write the variables to a text file, pass the values in this text file through a chunk of Fortran code and then get the results and plot them on a graph.

I can plot variables fine graphically (using GD and JpGraph) but what I really need to know is how to use PHP to write a standard text file.

For now I don't need to worry about the Fortran code, just how to pass variables to algorithms. For example, take all the data values and add 2 to each one, then get the output back into an array.

All help appreciated, and I am sorry if the question is slightly garbled. Thanks in advance.

- TatlarOkay, I have found I can use fopen to write to a text file. Is there any way of clearing the contents of the file before writing to it so it is empty every time I want to pass it through the later algorithm?

Cheers,

- Tatlarif (file_exists(SITE_PATH."/file.php")){
unlink("file.php");
}

you can delete it first.

but what do you mean you don't need to wory about fortran code but you want to pass variables to algorithms, with fortran or php?but what do you mean you don't need to wory about fortran code but you want to pass variables to algorithms, with fortran or php?


What I mean is that for now all I want to do is write the variable values to a .txt file. How this is dealt with by the Fortran algorithm is the author's job. However, I will be using the text file output of the algorithm to make a graph using JpGraph (which is awesome). But I am assuming I can just pull these values into an array and plot them simply.

Thanks for the prompt rely Scoutt.

- Tatlarwhen you get them out of the file they will already be in an array. so there you go.

and your welcomeWhat happens to that file if TWO users log in at the same time, and do different things?

Does the second user empty the file before the first user has finished using the data that was in it?Conflicts may arise indeed, but there's a function, flock(), to lock files for editing.

<!-- m --><a class="postlink" href="http://www.php.net/manual/en/function.flock.phpHi">http://www.php.net/manual/en/function.flock.phpHi</a><!-- m --> there,

After reading all your gracious replies I tried to implement flock into my script, but for some reason it does not want to work. Here is what does work (without flock):


// What is the filename we are writing to?
$file = "inmodel.txt";

// First check some variables were submitted
if( !$frequency | !$layers | !$depth ) {
print "You need to submit some variables first in ALL form fields!";
exit;
} else {
// Check the file exists
if( file_exists( $file ) ) {
unlink( $file ) ;
}
if( !$handle = fopen( $file, "w+" ) ) {
print "Cannot create the file ($file)";
exit;
}
if( !fwrite( $handle, "$str\n" ) ) {
print "Cannot write string ($str) to file ($file)";
exit;
}
fclose( $handle );
}


And here is what doesn't work (with flock):


// What is the filename we are writing to?
$file = "inmodel.txt";
$handle = fopen( $file, "w+" ) ;

// First check some variables were submitted
if( !$frequency | !$layers | !$depth ) {
print "You need to submit some variables first in ALL form fields!";
exit;
} else {
// Check the file exists
if( file_exists( $file ) ) {
unlink( $file ) ;
}
// Use flock to make sure only one
// person at a time accesses the text file
if( flock( $handle, LOCK_EX ) ) { // do an exclusive lock
if( !fwrite( $handle, "$str\n" ) ) {
print "Cannot write string ($str) to file ($file)";
exit;
}
flock( $handle, LOCK_UN ); // release the lock
} else {
echo "Couldn't lock the file ($file)!" ;
}
fclose( $handle );
}


Any ideas as to what I am doing wrong? My webserver is an Apple G5 running Panther 10.3.2 with Marc Liyanage's PHP.

Thanks in advance for your help.Anyone got any ideas about this? I really need a helping hand....

:(what errors are you getting? how do you know it isn't working?the inmodel.txt file does not update - so the whole thing is just not working. No errors thrown, which is weird???try it just like this

if (flock($handle)){ // do an exclusive lock

or

flock($handle); // do an exclusive lock

other than that I am not sure. you might not have permissions to run it. I will test it laterit also may be that the Mac OS X 10.3.2 Panther filesystem is not compatible.

From php.net:


Warning


flock() will not work on NFS and many other networked file systems. Check your operating system documentation for more details.

On some operating systems flock() is implemented at the process level. When using a multithreaded server API like ISAPI you may not be able to rely on flock() to protect files against other PHP scripts running in parallel threads of the same server instance!

flock() is not supported on antiquated filesystems like FAT and its derivates and will therefore always return FALSE under this environments (this is especially true for Windows 98 users).


Thanks for the help, I appreciate it.

:Dso it doesn't work in FAT, FAT32, or NTFS. so it seems that it will only work in *nix environments.

I forgot all about that.so it doesn't work in FAT, FAT32, or NTFS. so it seems that it will only work in *nix environments.

I forgot all about that.


... right, but OS X (Darwin) is based on FreeBSD and so should work. Troublesome....is your inmodel.txt read/write for all?


put
error_reporting(E_ALL);
to the top of the script


ps. nfs is the same as ntfs!it is owned by user 'www'. the directory and file are writeable by everyone.Originally posted by scoutt
try it just like this

if (flock($handle)){ // do an exclusive lock

or

flock($handle); // do an exclusive lock

other than that I am not sure. you might not have permissions to run it. I will test it later

Please excuse my ignorance ( :) ) but why would I even use flock if I don't do an exclusive lock (the LOCK_EX operation)?I beleive it defaults to that if you don't use it. so why have it. but don't quote me on it as I don't use flock, I use a db :POkay, so if flock is out of the question, does anyone have another PHP function that would lock a file so no more than one person can access it at the same time, in the context of the discussion thread?

All help appreciated!!

:)if the script that you tried flock, you unlink after opening the file...Does the file have to have a static name all of the time, the same name for every user?

If not, could you use a filename based on the date/time the session started, or on a session ID, or on a random number, or whatever?okay that is an option, but not ideal as:

1) the algorithm only takes input from the file inmodel.txt
2) I get a directory full of text files which I will have to write a shell script for cron to remove.

admittedly (2) is not a big deal, but (1) is the crux.....

thanks for the input...here is my suggestion, don't use flat files. use a db.Scoutt,

Thanks for the suggestion, but using a db would be overkill for just this one application. The model itself is Fortran-60(?) code that is custom developed by someone I am working with. There is no other interaction from any other files, just the inmodel.txt, model and outmodel.txt.

Correct me if I am wrong, but I don't see how setting up a db would be a time- or cost-effective way of solving this problem.

:hotbounce:db's don't have to be huge. sure it might be a little overkill but it will also solve all your problems. mysql and maybe others will lock the table if you want to have only person writing to it at a time.

your way is time consuming and worrysome. this whole time on this thread you could have had the db setup and running by now, but I can only give my opinion. it is your final decision that counts. :)

also take in account that I have no idea of the complexity of your project. if I am out of line I am sorry I just wanted to give my opinion. ;)No worries. As I always say, all comments welcome!!
:D
 
Back
Top