PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am working on a program that sends an array from an uploaded text file to a script that processes that array, and stores the results in a database. The problem is the text file is sometimes verry large and can take an hour or more to process. What I would like to do is send the array to process.php and have process.php run in the background, and ultimatly dispatch an email when the process is complete. Ive looked all over and either cant find anything that I can get working, or just plain dont understand how to get it to work. If anyone has any sugestions please let me know..
Here is the upload.php code that I use to send to process.php
In this example I am using a form to send the contents of the text file to process.php. In process.php I use preg_split to seperate the data into variables for processing.
trying find out if you can run cronjobs, it might be your solution.
a cronjob is designed to run at a certain time.
so you might not get the job done right away, but it will get done, on the server when the cronjob kicks in.
I'd couple the cron idea with adding the file to a queue of what files to process when the cron runs. This queue would be stored in the database somewhere, and would contain all information needed to process the file, who to email when it completes, status flags for the cron script..
Your upload script here, as it stands, loads the file into memory twice. Once with file_get_contents, and once with file().. there are ways of counting lines once a file is loaded with file_get_contents().. preg_match_all() for instance.
If you post process.php, we may be able to help speed up the script significantly. You may also consider using exec() or it's siblings, as they can be run in the background if called right.
Thanks for the quick reply's
I have thought about using cron but the problem is the data to be processed is not static. It would change on a daily basis. Something more like exec() would be more what Im looking for. Could popoen be another option? Ive looked at both of these and just dont seem to get how to get them to work.
I still havent got this figured out and Im hoping someone out there can point me in the right direction. I am using exec() to start the background script, but It doesnt seem to either send the array along with it or it just plain doesnt work..
process.php
<?
mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
@mysql_select_db ("database") or die( "Unable to select database");
foreach ($data as $value)
{
$query = "INSERT INTO linkpop VALUES ('','$value','')";
mysql_query($query);
}
mysql_close();
?>
Basicaly with this code Im just trying to upload a text file with names on it.
Then turn the file into an array, then send the array to process.php for processing in the background.
No luck as of yet...
Can any one help?
you have a php variable inside a single quote string. Single quote strings do not parse variables. Adding $data to a string will only insert 'Array' into the string. You need to compact the data back together into a usable form for the string use.
I dont understand what you mean. Should I not use preg_split to turn contents into an array before using exec()?
If I add preg_split() to process.php instead, I still get the same thing, one blank entry into the database.
Could you explain a little more about what you mean? Sorry Im prety new to php and get lost quite easily.
Would it be possible to save the array using sessions?