I have been struggling to extract text from a text file using a php script. The script below is the outcome of that struggle. It can handle multiple text files and extract all I need. Now i want to automate the process by creating a kind of interface to handle the upload and extraction. Below is the php script:
<?php
//Defining a search criteria
$searchCriteria = array(‘$GPGGA’);
//creating a reference for multiple text files in an array
$inFiles = array(‘track20100821.txt’,‘track20100821_high_sensitivity.txt’, ‘track20100821_low_sensitivity.txt’);
//processing individual files in the array called $inFiles via foreach loop
foreach($inFiles as $inFileName) {
//opening the input file and the output files
$inFiles = fopen($inFileName,“r”);
//This puts all the entries from the multiple text files into one distinct textfile
$outFile = fopen(“outputGGA.txt”, “w”);
//dirname($inFileName).basename($inFileName,‘.txt’).‘_out.txt’,“w”);
//reading the inFile line by line and outputting the line if searchCriteria is met
while(!feof($inFiles)) {
$line = fgets($inFiles);
$lineTokens = explode(‘,’,$line);
if(in_array($lineTokens[0],$searchCriteria)) {
fwrite($outFile,$line);
}
}
//close the 2 files
fclose($inFiles);
fclose($outFile);
}
?>
I have created an html form so i can upload the same textfile above in multiple uploads which looks like this:
</body>
</html> please you can assist in fine tuning this html not the best i know.
I want to upoad 2 or 3 text files and pass them to the php script above to do the extraction and write it to a text file.
My problem is that I do not know how to pass these uploaded file to the php script to handle them. I am also not sure of the php code to handle the multiple textfile upload. I did it for one upload but the upload was getting stored in my server instead of parsing it to the php script above.
Could someone please send me some code samples or give clues on how to achieve this.
Could someone please check this code for me to correctly pass in the uploaded file directory or suggest another alternative? I am stuck but wont give up on this!
This is what i am running now but the upload.html is different.
<?php
// This part uploads text files
if (isset($_POST['uploadfiles'])) {
if (isset($_POST['uploadfiles'])) {
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/Uploads/';
for ($i = 0; $i < count($_FILES['uploadedFile']['name']); $i++) {
//$number_of_file_fields++;
if ($_FILES['uploadedFile']['name'][$i] != '') { //check if file field empty or not
$number_of_uploaded_files++;
$uploaded_files[] = $_FILES['uploadedFile']['name'][$i];
//if (is_uploaded_file($_FILES['uploadedFile']['name'])){
if (move_uploaded_file($_FILES['uploadedFile']['tmp_name'][$i], $upload_directory . $_FILES['uploadedFile']['name'][$i])) {
$number_of_moved_files++;
}
}
}
}
echo "Files successfully uploaded . <br/>" ;
echo "Number of files submitted $number_of_uploaded_files . <br/>";
echo "Number of successfully moved files $number_of_moved_files . <br/>";
echo "File Names are <br/>" . implode(',', $uploaded_files);
/* This is the start of a script to accept the uploaded into another array of it own for processing.*/
$searchCriteria = array('$GPRMC');
//creating a reference for multiple text files in an array
$inFiles = array($_FILES['uploadedFile']['temp_name']);
//This opens a textfile for writing operation and puts all the entries from the multiple text files into one distinct textfile
$outFile = fopen("outputRMC.txt", "w");
$outFile2 = fopen("outputGGA.txt", "w");
//processing individual files in the array called $inFiles via foreach loop
foreach($inFiles as $inFileName) {
$numLines = 1;
//opening the input file
$inFiles = fopen($inFileName,"r");
//This line below initially was used to obtain the the output of each textfile processed.
//dirname($inFileName).basename($inFileName,'.txt').'_out.txt',"w");
//reading the inFile line by line and outputting the line if searchCriteria is met
while(!feof($inFiles)) {
$line = fgets($inFiles);
$lineTokens = explode(',',$line);
if(in_array($lineTokens[0],$searchCriteria)) {
if (fwrite($outFile,$line)===FALSE){
echo "Problem writing to file\
";
}
$numLines++;
}
// Defining search criteria for $GPGGA
$lineTokens = explode(',',$line);
$searchCriteria2 = array('$GPGGA');
if(in_array($lineTokens[0],$searchCriteria2)) {
if (fwrite($outFile2,$line)===FALSE){
echo "Problem writing to file\
";
}
}
}
echo "<p>For the file ".$inFileName." read ".$numLines;
//close the in files
fclose($_FILES['uploadedFile']['name']);
fflush($outFile);
fflush($outFile2);
}
fclose($outFile);
fclose($outFile2);
}
?>
I really help with this. Thanks you guys for some concern.
Looking at the exact code you posted, the line in red is inside a comment, so won’t be processed, otherwise it should work fine I think. A couple of lines above you’ve got /* which will continue to comment (and ignore) everything until */
Thanks for your quick reply. I would try out what you said but the fact is my multiple upload form is not working. I succeeded with only one upload. Do you please have any sample code apart from this? I think your suggestion using uploadedFile1, uploadedFile2 is what should be. am really struggling here to get this up. any more suggestions would be highly appreciated
Information about the uploaded files will be in an array called $_FILES so after uploading try this to see what you’ve got:
echo '<pre>';
print_r($_FILES);
echo '</pre>';
I’ve never tried using ‘uploadedFile’ as the name, I always give each file a unique name (eg. uploadedFile1, uploadedFile2, etc.), so I don’t know if that works.
Anyway, the location of the uploaded file should be set in something like $_FILES[‘uploadedFile1’][‘tmp_name’], so you should be able to feed that into your script and process it. Or it might be better to move the file to a folder on your server first using move_uploaded_file.
Hello guys, I think i have been able to get over with this and this is what I did in case some other newbie like me may face similar issue. The change to the whole sample codes i have uploaded here is not much and revolves around the following changes:
That was all that mattered. Hope someone finds it useful at least. At last i can move on to more important things. many thanks to you all for the assistance.
sorry Mart for that, still missing out the ‘tmp_name’. I have corrected the whole thing and there are no errors now. the script is uploading the files but the code below which picks up the files is still not functional. it is creating the text files it is suppose to write to but they are empty. Below is the complete code am running maybe you could still pick out some loopholes. Am not tired. Sorry for bothering you guys. This is a hard one for me to crack.
<?php
// This part uploads text files
if (isset($_POST['uploadfiles'])) {
if (isset($_POST['uploadfiles'])) {
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/Uploads/';
for ($i = 0; $i < count($_FILES['uploadedFile']['name']); $i++) {
//$number_of_file_fields++;
if ($_FILES['uploadedFile']['name'][$i] != '') { //check if file field empty or not
$number_of_uploaded_files++;
$uploaded_files[] = $_FILES['uploadedFile']['name'][$i];
//if (is_uploaded_file($_FILES['uploadedFile']['name'])){
if (move_uploaded_file($_FILES['uploadedFile']['tmp_name'][$i], $upload_directory . $_FILES['uploadedFile']['name'][$i])) {
$number_of_moved_files++;
}
}
}
}
echo "Files successfully uploaded . <br/>" ;
echo "Number of files submitted $number_of_uploaded_files . <br/>";
echo "Number of successfully moved files $number_of_moved_files . <br/>";
echo "File Names are <br/>" . implode(',', $uploaded_files);
/* This is the start of a script to accept the uploaded into another array of it own for processing.*/
$searchCriteria = array('$GPRMC');
//creating a reference for multiple text files in an array
$inFiles = ($_FILES['uploadedFile']['tmp_name']);
//This opens a textfile for writing operation and puts all the entries from the multiple text files into one distinct textfile
$outFile = fopen("outputRMC.txt", "w");
$outFile2 = fopen("outputGGA.txt", "w");
//processing individual files in the array called $inFiles via foreach loop
if (is_array($inFiles)) {
foreach($inFiles as $inFileName) {
$numLines = 1;
//opening the input file
$inFiles = fopen($inFileName,"r");
//This line below initially was used to obtain the the output of each textfile processed.
//dirname($inFileName).basename($inFileName,'.txt').'_out.txt',"w");
//reading the inFile line by line and outputting the line if searchCriteria is met
while(!feof($inFiles)) {
$line = fgets($inFiles);
$lineTokens = explode(',',$line);
if(in_array($lineTokens[0],$searchCriteria)) {
if (fwrite($outFile,$line)===FALSE){
echo "Problem writing to file\
";
}
$numLines++;
}
// Defining search criteria for $GPGGA
$lineTokens = explode(',',$line);
$searchCriteria2 = array('$GPGGA');
if(in_array($lineTokens[0],$searchCriteria2)) {
if (fwrite($outFile2,$line)===FALSE){
echo "Problem writing to file\
";
}
}
}
}
echo "<p>For the file ".$inFileName." read ".$numLines;
//close the in files
fclose($_FILES['uploadedFile']['tmp_name']);
fflush($outFile);
fflush($outFile2);
}
fclose($outFile);
fclose($outFile2);
}
?>
As mentioned previously, ‘temp_name’ needs to be ‘tmp_name’. That will solve the second error, but it’s probably a good idea to check that $inFiles is an array before the foreach, eg:
if (is_array($inFiles)) {
foreach($inFiles as $inFileName) {
...
}
}
Like I said in my earlier post. the above code is working and multiple files are uploaded. There are no errors.
This is the uploadManager. php to process it:
.
..
...
if (isset($_POST['uploadfiles'])) {
if (isset($_POST['uploadfiles'])) {
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/Uploads/';
for ($i = 0; $i < count($_FILES['uploadedFile']['name']); $i++) {
//$number_of_file_fields++;
if ($_FILES['uploadedFile']['name'][$i] != '') { //check if file field empty or not
$number_of_uploaded_files++;
$uploaded_files[] = $_FILES['uploadedFile']['name'][$i];
//if (is_uploaded_file($_FILES['uploadedFile']['name'])){
if (move_uploaded_file($_FILES['uploadedFile']['tmp_name'][$i], $upload_directory . $_FILES['uploadedFile']['name'][$i])) {
$number_of_moved_files++;
}
}
}
}
echo "Files successfully uploaded . <br/>" ;
echo "Number of files submitted $number_of_uploaded_files . <br/>";
echo "Number of successfully moved files $number_of_moved_files . <br/>";
echo "File Names are <br/>" . implode(',', $uploaded_files);
}
What I have done is to merge the extraction script with the uploadmanager.php into one script as shown below:
<?php
...
...
if (isset($_POST['uploadfiles'])) {
if (isset($_POST['uploadfiles'])) {
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/Uploads/';
for ($i = 0; $i < count($_FILES['uploadedFile']['name']); $i++) {
//$number_of_file_fields++;
if ($_FILES['uploadedFile']['name'][$i] != '') { //check if file field empty or not
$number_of_uploaded_files++;
$uploaded_files[] = $_FILES['uploadedFile']['name'][$i];
//if (is_uploaded_file($_FILES['uploadedFile']['name'])){
if (move_uploaded_file($_FILES['uploadedFile']['tmp_name'][$i], $upload_directory . $_FILES['uploadedFile']['name'][$i])) {
$number_of_moved_files++;
}
}
}
}
echo "Files successfully uploaded . <br/>" ;
echo "Number of files submitted $number_of_uploaded_files . <br/>";
echo "Number of successfully moved files $number_of_moved_files . <br/>";
echo "File Names are <br/>" . implode(',', $uploaded_files);
/* This is the start of a script accept the uploaded into another array of it own for processing.
$searchCriteria = array('$GPRMC');
//creating a reference for multiple text files in an array
[COLOR="Red"]$inFiles = array($_FILES['uploadedFile']['tmp_name']);[/COLOR]
//This opens a textfile for writing operation and puts all the entries from the multiple text files into one distinct textfile
$outFile = fopen("outputRMC.txt", "w");
$outFile2 = fopen("outputGGA.txt", "w");
//processing individual files in the array called $inFiles via foreach loop
foreach($inFiles as $inFileName) {
$numLines = 1;
//opening the input file
$inFiles = fopen($inFileName,"r");
//This line below initially was used to obtain the the output of each textfile processed.
//dirname($inFileName).basename($inFileName,'.txt').'_out.txt',"w");
//reading the inFile line by line and outputting the line if searchCriteria is met
while(!feof($inFiles)) {
$line = fgets($inFiles);
$lineTokens = explode(',',$line);
if(in_array($lineTokens[0],$searchCriteria)) {
if (fwrite($outFile,$line)===FALSE){
echo "Problem writing to file\
";
}
$numLines++;
}
...
...
...
?>
In the red hilighted line above I sticked the the $_Files… into the array of the extraction script.
This is not working. the files are getting uploaded though but the $_Files… which I have passed into the “$infiles” seem not to be working.
This is where I am and hope some body can help here. meanwhile i would continue to see how I can figure this out.
I have come across this “if (is_uploaded_file($_FILES[‘uploadedFile’][‘name’])){” and I passed the same array into the “$infiles” but it did not do any magic.
I would really appreciate some concern.
Many thanks
Hi, Sorry I have been away. took ill but am now back. To anwer your question:
I do not get any error when i do the print_r($FILES). The files are being moved to a directory i specified on the server. Now i have succeeded in uploading the files but struggling to pass the array into the next script. this is just to ans you but would post again where i am on this. thanks
Upload.html code for multiple file uploads:
Array
if (isset($_POST['uploadfiles'])) {
if (isset($_POST['uploadfiles'])) {
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/Uploads/';
for ($i = 0; $i < count($_FILES['uploadedFile']['name']); $i++) {
//$number_of_file_fields++;
if ($_FILES['uploadedFile']['name'][$i] != '') { //check if file field empty or not
$number_of_uploaded_files++;
$uploaded_files[] = $_FILES['uploadedFile']['name'][$i];
//if (is_uploaded_file($_FILES['uploadedFile']['name'])){
if (move_uploaded_file($_FILES['uploadedFile']['tmp_name'][$i], $upload_directory . $_FILES['uploadedFile']['name'][$i])) {
$number_of_moved_files++;
}
}
}
}
echo '<pre>';
print_r($_FILES);
echo '</pre>';
echo "Files successfully uploaded . <br/>" ;
echo "Number of files submitted $number_of_uploaded_files . <br/>";
echo "Number of successfully moved files $number_of_moved_files . <br/>";
echo "File Names are <br/>" . implode(',', $uploaded_files);
Hmm, according the the manual it should work fine with an array as you’ve got it, but you could try giving each file input a unique name. I know that definitely works as I’ve done it many times. Then you just need to reference each file separately, eg:
That’ll create an array with the temporary filenames of the uploaded files which I think you can then process, but hopefully someone else can clarify whether the temp files can be processed directly or whether they need to be moved first.
Are you getting any errors when you upload multiple files? What’s the output of “print_r($_FILES);”?