A peculiar newbie debugging
<p>It is one of the things I should have the know how but it really bugs. I have a large script which automates serveral tasks. It starts with uploading of files after which data from the files are extracted and then imported into mysql. In debugging mode it works perfectly but on my web front-end, execution stops after the data from the uploaded file is extracted. It never reaches the importation into database phase.</p>

<p>I realised that I had used this particular code below which I hold suspect:</p>

<pre><code>$newFilePath = $upload_directory."/".$inFileName;
//opening the input file
if($inFileName != "")
$inputFile = fopen($newFilePath,"r");
else exit(1);
if(!$inputFile)
exit(0);
while(!feof($inputFile)) {
</code></pre>

<p>It is obvious that the exit() as used terminates the scripts thereby leaving out the lines that is handling the import of data into mysql.WIth the above, the upload works and the data separation carried out but the import never got executed. After some study I reviewed that portion by doing something quite appropriate like:</p>

<pre><code>if (file_exists($inFileName)){
        $inputFile = fopen($newFilePath,"r");
    }
    else {
        echo "No file found ";
    }

    if (is_readable($inputFile)){
        echo " ";
    }else {
        echo 'The file could not be read';
    }   
</code></pre>

<p>Now this piece of code review did not get me anyway as the upload is not even possible.</p>

<p>Now my problem is to fix this little portion of the code so I get the other parts of the script after it to do the import. It has been a nightmare for a beginner like me. I would appreciate if someone could review the above portion in a different way. I hope to see something that is similar or different but valid. I have learnt tougher stuff than this but this is simply nuts. I hope someone could understand my explanation. Thanks</p>

<h2>New Edit.</h2>

<pre><code>if (is_array($inFilesArray)) {
foreach($inFilesArray as $inFileName) {

    $numLines = 1;

    $newFilePath = $upload_directory."/".$inFileName;
    //opening the input file
    if (file_exists($newFilePath)){

        if (is_readable($newFilePath)){

            $inputFile = fopen($newFilePath,"r");
        }
    } else {
        echo "File not accessable";
    }

    //reading the inFile line by line and outputting the line if searchCriteria is met
    while(!feof($inputFile)) {
        $line = fgets($inputFile);
        $lineTokens = explode(',',$line);
        // Assign Device ID
        switch ($lineTokens[0]){
            case "IMEI 358998018395510\r\n":
                $device_id = 1;
                break;
            case "IMEI 352924028650492\r\n":
                $device_id = 3;
                break;
            case '$GPVTG':
                $device_id = 2;
                break;

        }
        if(in_array($lineTokens[0],$searchCriteria)) {
            if (fwrite($outFile4, $device_id . "," .$line)===FALSE){
                echo "Problem writing files\n";
            }
            $time = $lineTokens[1];
            $date = $lineTokens[9];
            $numLines++;
        }

        // Defining search criteria for $GPGGA
        $lineTokens = explode(' ',$line);
        $searchCriteria2 = array('OutCell');
        if(in_array($lineTokens[0],$searchCriteria2)) {
            if (fwrite($outFile5, $time.",".$date."\n")===FALSE){
                echo "Problem writing to file\n";
            }
        }
    }

    fclose($inputFile);
}
</code></pre>

<h2>Entire Script</h2>

<pre><code>&lt;?php

#This script has threee parts: The first handles the uploaded files while the second   part retrieves all RMCs and Handover dates and the 3rd handles importation of the data  into their respective table in mysql database.

# This part uploads text files
include 'setamainfunctions.php';

if (isset($_POST['uploadfiles'])) {
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/Uploads/';
//echo count($_FILES['uploadedFile']['name'])." uploaded ";
for ($i = 0; $i &lt; 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 "Number of files submitted $number_of_uploaded_files . &lt;br/&gt;";
echo "Number of successfully moved files $number_of_moved_files . &lt;br/&gt;";
echo "File Names are &lt;br/&gt;" . implode("&lt;br/&gt;", $uploaded_files)."\n";
echo "&lt;/br&gt;";
echo "&lt;p&gt; Please find the processed RMCs and corresponding handovers as text files named:outputRMCs.txt and OutputHandovers.txt in the Setapro project root folder&lt;/p&gt;";
/*  echo "&lt;br/&gt;";
 echo "&lt;br/&gt;";
 echo "&lt;p&gt; Result of Mobile GPRMCs Transaction";
 //echo "Total entries imported:.$numlines . &lt;br/&gt;";
 echo "Data extraction and import mobile mobile GPRMCs successfuly completed";
 echo "&lt;br/&gt;";
 echo "&lt;br/&gt;";
 echo "&lt;p&gt; Result of Handover Transaction";
 //echo "Total entries imported:.$numlines . &lt;br/&gt;";
 echo "Data extraction and imports for mobile handovers successfuly completed";*/
</code></pre>

<p>}</p>

<pre><code># This is the start of a script which accepts the uploaded into another array of it   own for extraction of GPRMCs and handover dates.

$searchCriteria = array('$GPRMC');

//creating a reference for multiple text files in an array

/*if(isset($_FILES['uploadedFile']['name']))
 $_FILES['uploadedFile'] = '';
 }else {
echo "yes";
}*/
$inFilesArray = ($_FILES['uploadedFile']['name']);

//$inFiles =    fopen($_FILES['uploadedFile']['tmp_name'], 'r');

//This opens a textfile for writing operation and puts all the entries from the      multiple text files into one distinct textfile
if( ($outFile4 = fopen("outputRMCs.txt", "a")) === false ){
echo " ";
}
$outFile5 = fopen("outputHandovers.txt", "a");

//processing individual files in the array called $inFilesArray via foreach loop

if (is_array($inFilesArray)) {
foreach($inFilesArray as $inFileName) {

    $numLines = 1;

    $newFilePath = $upload_directory."/".$inFileName;
    $correctFile = false;
    //opening the input file
    if (file_exists($newFilePath)){

        if (is_readable($newFilePath)){

            $inputFile = fopen($newFilePath,"r");
            $correctFile = true;
        }
    }
    if (!$correctFile)
    echo "File not accessable";

    //reading the inFile line by line and outputting the line if  searchCriteria is met
    while(!feof($inputFile)) {
        $line = fgets($inputFile);
        $lineTokens = explode(',',$line);
        // Assign Device ID
        switch ($lineTokens[0]){
            case "IMEI 358998018395510\r\n":
                $device_id = 1;
                break;
            case "IMEI 352924028650492\r\n":
                $device_id = 3;
                break;
            case '$GPVTG':
                $device_id = 2;
                break;

        }
        if(in_array($lineTokens[0],$searchCriteria)) {
            if (fwrite($outFile4, $device_id . "," .$line)===FALSE){
                echo "Problem writing files\n";
            }
            $time = $lineTokens[1];
            $date = $lineTokens[9];
            $numLines++;
        }

        // Defining search criteria for $GPGGA
        $lineTokens = explode(' ',$line);
        $searchCriteria2 = array('OutCell');
        if(in_array($lineTokens[0],$searchCriteria2)) {
            if (fwrite($outFile5, $time.",".$date."\n")===FALSE){
                echo "Problem writing to file\n";
            }
        }
    }

    fclose($inputFile);
}

//close the in files
fflush($outFile4);
fflush($outFile5);
}


fclose($outFile4);
fclose($outFile5);

#End of script for handling extraction

# This is the start of the script which imports the text file data into mysql  database   and does the necessary conversion.

$FilePath1 = $_SERVER["DOCUMENT_ROOT"] . '/setapro/outputRMCs.txt';

if (is_readable($FilePath1)) {
$handle = fopen($FilePath1, "r");
rmc_handoverHandler($handle);
echo "";
fclose($handle);
} else {
echo 'The file could not be read';
</code></pre>

<p>}</p>

<pre><code>#Begining of script to handle imports into handover table

$FilePath2 = $_SERVER["DOCUMENT_ROOT"].'/setapro/outputHandovers.txt';
if (is_readable($FilePath2)) {
$handle = fopen($FilePath2, "r");
mobile_handoverHandler($handle);
echo "";
fclose($handle);
} else {
echo 'The file could not be read';
}

#End of script for handling imports into handover table

?&gt;
</code></pre>
