SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face I need help passing text files to my php text extraction script.

    Hello all,

    I am very new to php and need help

    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:
    -------------------------
    html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; UTF-8" />
    <title>Upload Form</title>
    </head>

    <body>

    <form action="uploadmanager.php" method="post" enctype="multipart/form-data"><input type="hidden" name="MAX_FILE_SIZE" value="500000" />
    Please choose files to uploadbr />
    <input name="uploadedFile[]" type="file" /><br />
    <input name="uploadedFile[]" type="file" /><br />
    <input type="submit" value="Upload files" />
    </form>/>
    </form>

    </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.

    Best Regards

    Paul

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Surrey, UK
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Paul,

    Information about the uploaded files will be in an array called $_FILES so after uploading try this to see what you've got:

    PHP Code:
    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.

    Hope this helps - sorry if I'm a bit vague

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Martbean,

    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

    cheers
    paul

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Surrey, UK
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    PHP Code:
    $inFiles = array();
    $inFiles[] = $_FILES['uploadedFile1']['tmp_name'];
    $inFiles[] = $_FILES['uploadedFile2']['tmp_name'];
    etc... 
    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);"?

  5. #5
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
     
    "uploadedFile[]"
    is fine for the name.

    That is saying you are sending an array of files called uploadedFile to uploadmanager.php

    Your form looks fine as far as I can tell. Use the code tag in the future (# - in the editor's tool bar) so your posted code is easier to read.

    The problem is most probably in uploadmanager.php

    You will need something similar to

    Code:
     
    foreach($_FILES['txtUploadFile']['error'] as $key => $value) {
        if($_FILES['txtUploadFile']['name'][$key] != '') {
            echo '<p><hr />Uploading File: '.$_FILES['txtUploadFile']['name'][$key].'</p>';
    .
    .
    .
    in uploadmamanger.php to upload the multiple files

  6. #6
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by martbean View Post
    Hi Paul,

    Information about the uploaded files will be in an array called $_FILES so after uploading try this to see what you've got:

    PHP Code:
    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.

    Hope this helps - sorry if I'm a bit vague
    Quote Originally Posted by martbean View Post
    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:

    PHP Code:
    $inFiles = array();
    $inFiles[] = $_FILES['uploadedFile1']['tmp_name'];
    $inFiles[] = $_FILES['uploadedFile2']['tmp_name'];
    etc... 
    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);"?
    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:
    Code:
    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);
    And the result of the above is this:

    Array
    (
    [uploadedFile] => Array
    (
    [name] => Array
    (
    [0] => track20100608.txt
    [1] => track20100610.txt
    [2] =>
    [3] =>
    [4] =>
    [5] =>
    [6] =>
    [7] =>
    [8] =>
    [9] =>
    [10] =>
    [11] =>
    )

    [type] => Array
    (
    [0] => text/plain
    [1] => text/plain
    [2] =>
    [3] =>
    [4] =>
    [5] =>
    [6] =>
    [7] =>
    [8] =>
    [9] =>
    [10] =>
    [11] =>
    )

    [tmp_name] => Array
    (
    [0] => C:\wamp\tmp\php9D5.tmp
    [1] => C:\wamp\tmp\php9D6.tmp
    [2] =>
    [3] =>
    [4] =>
    [5] =>
    [6] =>
    [7] =>
    [8] =>
    [9] =>
    [10] =>
    [11] =>
    )

    [error] => Array
    (
    [0] => 0
    [1] => 0
    [2] => 4
    [3] => 4
    [4] => 4
    [5] => 4
    [6] => 4
    [7] => 4
    [8] => 4
    [9] => 4
    [10] => 4
    [11] => 4
    )

    [size] => Array
    (
    [0] => 99773
    [1] => 115098
    [2] => 0
    [3] => 0
    [4] => 0
    [5] => 0
    [6] => 0
    [7] => 0
    [8] => 0
    [9] => 0
    [10] => 0
    [11] => 0
    )

    )

    )

    Files successfully uploaded .
    Number of files submitted 2 .
    Number of successfully moved files 2 .
    File Names are
    track20100608.txt,track20100610.txt

  7. #7
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_paisley View Post
    Hello all,

    I am very new to php and need help

    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:
    -------------------------
    html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; UTF-8" />
    <title>Upload Form</title>
    </head>

    <body>

    <form action="uploadmanager.php" method="post" enctype="multipart/form-data"><input type="hidden" name="MAX_FILE_SIZE" value="500000" />
    Please choose files to uploadbr />
    <input name="uploadedFile[]" type="file" /><br />
    <input name="uploadedFile[]" type="file" /><br />
    <input type="submit" value="Upload files" />
    </form>/>
    </form>

    </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.

    Best Regards

    Paul
    Dear friends, sorry to bother you again on this issue. I have been away due to illness and am back to continue where I stopped.

    Below is the Upload.html code I have:

    Code:
    <form action="processFiles.php" method="post" enctype="multipart/form-data"><input type="hidden" name="MAX_FILE_SIZE" value="500000" />
      Please choose files to upload:<br />
      <input name="uploadedFile[]" type="file" /><br />
      <input name="uploadedFile[]" type="file" /><br />
      <input name="uploadedFile[]" type="file" /><br />
      <input name="uploadedFile[]" type="file" /><br />
      <input type="submit" name="uploadfiles" value="Upload files" />
    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:
    .
    Code:
    ..
    ...
     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:


    Code:
    <?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
    
    $inFiles = array($_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
    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\n";	
    			 }
    			 $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

    Paul

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Surrey, UK
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Paul,

    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 */
    Martin.

  9. #9
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Martin for pointing that out. i am going to check it now and fix it hope it works on correcting the comment.

  10. #10
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry

    Quote Originally Posted by paul_paisley View Post
    Dear friends, sorry to bother you again on this issue. I have been away due to illness and am back to continue where I stopped.

    Below is the Upload.html code I have:

    Code:
    <form action="processFiles.php" method="post" enctype="multipart/form-data"><input type="hidden" name="MAX_FILE_SIZE" value="500000" />
      Please choose files to upload:<br />
      <input name="uploadedFile[]" type="file" /><br />
      <input name="uploadedFile[]" type="file" /><br />
      <input name="uploadedFile[]" type="file" /><br />
      <input name="uploadedFile[]" type="file" /><br />
      <input type="submit" name="uploadfiles" value="Upload files" />
    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:
    .
    Code:
    ..
    ...
     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:


    Code:
    <?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
    
    $inFiles = array($_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
    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\n";	
    			 }
    			 $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

    Paul
    Hello guys,

    This is frustrating for a newbie! I have checked the code ones again and the files are uploading as usual but it still does not work. this line:
    Code:
    $inFiles = array($_FILES['uploadedFile']['temp_name']);
    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.

    Code:
    <?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\n";	
    			 }
    			 $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\n";	
    			 }
    		}
    	}
    
    	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.

    Regards

  11. #11
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Surrey, UK
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_paisley View Post
    Code:
    $inFiles = array($_FILES['uploadedFile']['temp_name']);
    The last part of this should be 'tmp_name', as you had it originally. You also don't need array() as the $_FILES... part is already an array
    Martin.

  12. #12
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Mart, i would see to that

  13. #13
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mart,

    This is the line am using here now:
    $inFiles = ($_FILES['uploadedFile']['temp_name']);

    foreach($inFiles as $inFileName) {

    $numLines = 1;
    //opening the input file
    $inFiles = fopen($inFileName,"r");

    but I have the following errors corresponding to the two above highlited lines.

    1. Notice: Undefined index: temp_name in C:\wamp\www\setapro\processFiles.php on line 38

    2. Warning: Invalid argument supplied for foreach() in C:\wamp\www\setapro\processFiles.php on line 45

    Those are the errors i am having. The battle is still not over yet!

    I would still appreciate some help.

  14. #14
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Surrey, UK
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Paul,

    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:

    Code:
    if (is_array($inFiles)) {
     foreach($inFiles as $inFileName) {
      ...
     }
    }
    Martin.

  15. #15
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry

    Quote Originally Posted by martbean View Post
    Hi Paul,

    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:

    Code:
    if (is_array($inFiles)) {
     foreach($inFiles as $inFileName) {
      ...
     }
    }
    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.


    Code:
    <?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\n";	
    			 }
    			 $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\n";	
    			 }
    		}
    	}
    }
    
    	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);
    }
     
    ?>

  16. #16
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Quote Originally Posted by paul_paisley View Post
    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.


    Code:
    <?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\n";	
    			 }
    			 $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\n";	
    			 }
    		}
    	}
    }
    
    	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);
    }
     
    ?>
    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:

    After the html uploads the files, I use this
    Code:
    $inFilesArray = ($_FILES['uploadedFile']['name']);
    to access the uploaded file names. and secondly, created this
    Code:
    $newFilePath = $upload_directory."/".$inFileName;
    In short this is an excerpt from the whole code where the magic happened.

    Code:
    $inFilesArray = ($_FILES['uploadedFile']['name']);
            $outFile = fopen("outputRMC.txt", "w");
    	$outFile2 = fopen("outputGGA.txt", "w");
    		
           if (is_array($inFilesArray)) {
    		foreach($inFilesArray as $inFileName) {
    			
    			$numLines = 1;
    			
    			$newFilePath = $upload_directory."/".$inFileName;
    			
    			if($inFileName != "")
      				$inputFile = fopen($newFilePath,"r");
    			else exit(1);
    						
    			if(!$inputFile)
    				exit(0);
    			while(!feof($inputFile)) {
    				$line = fgets($inputFile);
    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.

    Regards

  17. #17
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Surrey, UK
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, glad you got it sorted!
    Martin.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •