Forum Moderators: coopster
<?php
$array = array('John Doe', 'Denver', 'Colorado');
list($name, $city, $state) = $array;
?>
So now my journey continues...
I've now created a PHP form that takes the text in a .txt file and turns it into a string. I can then process that string as I did the previous information.
My goal here is to have, say, 100 files with data like this stored in each. I'd like to have my PHP form check the directory where the file is stored, open the first file in the directory and use its data. The data would fill my form, so I could look it over and then submit it to a database.
How could I get PHP to grab the first file, read it, and delete it after its done so that after adding to the database, the form would reload with the NEW first file in the directory?
<?php
//get first file
$count = $count + 1;
$firstfile = "test".$count.".txt";
// get contents of a file into a string
$filename = $firstfile;
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
//split string
$contents = nl2br($contents);
$contents = str_replace("<br />", ":", $contents);
$contents = str_replace(": ".": ", ":", $contents);
$newcontents = split(":", $contents);
//delete file
// unlink($filename);
?>
I've commented out the delete option because I'm not sure I want to use it yet.
Also, in my form I have a hidden field that contains the $count variable so that it passes through with each form submission.
Can anyone see any problems I may run into with this method?
I was just writing an answer, I came up with something like this
<?
$mydir = "/path/to/dir/";
$d = dir($mydir);
while($entry = $d->read()) {
if ($entry!= "." && $entry!= "..") {
$myfile = $mydir . $entry;
$fp = fopen($myfile, "r");
// do your importing of the file contents here
fclose($fp);
unlink($myfile);
}
}
$d->close();
?>
My goal here is to scan paper documents and have image recognition software convert them into text files (only certain fields based on a template).
Then, I can upload these files to my webserver, where my PHP App will autoload each file into the record insertion form. This will allow easy conversion of paper documents to database.
My first solution works, but I will have to see how my scanner program names files before I'll know for sure. Dependant on that, I may have to use your idea.
Thanks for your help!