Solved

How do I make sure a file is processed before it is altered?

Posted on 2011-02-16
12
229 Views
Last Modified: 2012-06-27
A remote server sends data to me (POST). I have a PHP script (receive.php) that receives the data and store it in a file. Then, a totally different script (process.php) reads that file, process its content and empty the file (this way it will prepare the file for the next write and it won't process the same data twice).

But the remote server may send data to my receive.php script while process.php script is still processing the file. How do I solve this problem?
0
Comment
Question by:cubic77
12 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
0
 
LVL 4

Expert Comment

by:florjan
Comment Utility
Php file operators. So check if file is not null. If it isnt write to other files. You could have a for sentence for less code. If you need anymore help I'll help you as soon as I come home.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
A queue is your best bet.

Instead of allowing the scripts to access the file directly, get them to write into a table which holds the file's name and the next operation as well as any data. A separate script can then be run on a schedule to inspect the queue and process any work

-- UNTESTED --

CREATE TABLE processQueue (

  id INT NOT NULL auto_increment,
  filename VARCHAR(255) NOT NULL,
  command VARCHAR(32) NOT NULL,
  data TEXT NOT NULL,
  PRIMARY KEY(id)  

);


Then INSERT INTO processQueue ( filename, command, data ) VALUES (........ etc.....);

Have your scheduled script pull off the oldest record

SELECT * FROM processQueue ORDER BY filename, id LIMIT 1

apply the changes and then remove the work record

DELETE FROM processQueue where id = $id

This ensures that all operations on a file are performed sequentially. Have the job run every few minutes and if the queue is empty then just terminate the job. If you know that the changes will take fraction of a second then you might decide to process up to 10 commands or whatever works for you before you run out of processor time.

0
 
LVL 8

Expert Comment

by:rationalboss
Comment Utility
Using a database will be helpful. But if you don't want to use a database, you can try the following.

Since you might still want to allow your application to receive updates even though there is still a file being processed, just allow the page to continue receiving requests. (that means, not locking the file)

My idea is to create a temporary directory that contains files you want processed. For example, you can create a queue/ directory. Each time a file is received, you put a file in that directory with a random file name. You can use file_put_contents() or move_uploaded_file() or simply rename(), you weren't able to mention how you receive files in receive.php.


Now, the other file which processes these files, loops to check for files in that directory:
<?php
$dir = "queue/";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false && filetype($dir . $file) == 'file') {
            // this is a file in the queue directory. Now, do something with $dir.$file
            // and make sure you delete the file after to indicate you're done processing it:
            // unlink($dir.$file);
        }
        closedir($dh);
    }
}
?>

... and delete the file after processing it. This way, you can accept multiple files for processing and have it processed sequentially and you don't need to use a database. The readdir() function would return the files in the order in which they are created in the file system.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
This sounds like a strange application design.  Regarding this, "a totally different script (process.php) reads that file" -- how does that script get started?  And why would you want to re-use the file name?  Why not have the receive.php script collect the data, write the file, and then start the process.php script?  You would want to use a different file name (maybe using a timestamp) for each received data set.  Once process.php has completed its work, it could unlink() the file.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:cubic77
Comment Utility
@Ray_Paseur

> Why not have the receive.php script collect the data, write the file, and then start the process.php script?
Actually this is what I am doing right now.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
If you're doing it that way and you use a different file name for each new file, you should be on firm ground, at least with this end of the application.
0
 

Author Comment

by:cubic77
Comment Utility
>If you're doing it that way and you use a different file name for each new file, you should be on firm ground
Indeed it may be the best approach. Also, this way I can remove the need of using a data base to store my data. I can read the data directly from those files.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
That may be true.  Not sure what kind of data you have, but larger "blobs" of information like images or video are better in the file system, whereas smaller, succinct, searchable pieces seem to be most useful in a data base.  It's part art and part science ;)
0
 

Accepted Solution

by:
cubic77 earned 0 total points
Comment Utility
I totally agree with databases. However, not storing the data in a DB has an important advantage to me. I can move the application (PHP script) and associated data to any server I want in less than 60 seconds. No DB setup nightmare.
0
 

Author Closing Comment

by:cubic77
Comment Utility
no other solid solution provided
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Router for PHP reqeusts 12 29
FTP File permissions 1 27
Export MYSQL to .CSV Add Column Names PHP 2 21
.htaccess file settings 4 31
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now