I need some help with debugging a small portion of my code. I am still learing PHP.
I have a single script which handles the upload of files, the uploaded files are processed and imported into a database automatically.
In debugging mode everything works perfectly but during normal runtime the files only get uploaded and extracted but the script never executes the lines that follow. These lines handle the importation of data.
I realised that this portion of my code is suspect:
I do know that, the exit() used above terminates the script execution but what I don’t understand is why the lines following the exit() up to
fclose($inputFile);
are working perfectly. This is not the problem. The problem is with the lines after the fclose($inputFile);
After some reading I changed the exit part above to:
if (file_exists($newFilePath)){
$inputFile = fopen($newFilePath,"r");
}
else {
echo "No file found ";
}
if (is_readable($inputFile)){
echo " ";
}else {
echo 'The file could not be read';
}
This in my opinion is should be done but it just does not help the matter. I would appreciate a second look.
It is my thinking that the exit() as used causes the remaining part of the script not to execute after a user uploads a file
Thanks Kalon. Sorry for getting back late. I have been reviewing that part of the code to remove the exit().
Below is the whole script excluding function calls.
<?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 < 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 . <br/>";
echo "Number of successfully moved files $number_of_moved_files . <br/>";
echo "File Names are <br/>" . implode("<br/>", $uploaded_files)."\
";
echo "</br>";
echo "<p> Please find the processed RMCs and corresponding handovers as text files named:outputRMCs.txt and OutputHandovers.txt in the Setapro project root folder</p>";
/* echo "<br/>";
echo "<br/>";
echo "<p> Result of Mobile GPRMCs Transaction";
//echo "Total entries imported:.$numlines . <br/>";
echo "Data extraction and import mobile mobile GPRMCs successfuly completed";
echo "<br/>";
echo "<br/>";
echo "<p> Result of Handover Transaction";
//echo "Total entries imported:.$numlines . <br/>";
echo "Data extraction and imports for mobile handovers successfuly completed";*/
}
# 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\
":
$device_id = 1;
break;
case "IMEI 352924028650492\\r\
":
$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\
";
}
$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."\
")===FALSE){
echo "Problem writing to file\
";
}
}
}
fclose($inputFile);
}
//close the in files
fflush($outFile4);
fflush($outFile5);
}
fclose($outFile4);
fclose($outFile5);
#End of script for handling extraction
[COLOR="Red"]# This is the start of the script which imports the text file data into mysql [/COLOR]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';
}
#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
?>
The script from the red highlited line does not execute as upper part handles the upload and processing of the data therein. My surprise is, if paste the script pathname in my address bar it runs and does the import but it does run so when a user clicks a form to upload the file. The form triggers the php script. There is something causing execution not to get to the end and the script some how times out and returns 0 when it is suppose to echo the number of files submitted and other user messages. This is my headache. I am not sure what is run.
to be honest, there is too much code there for me to get my head around when I can’t actually run the code without setting up a test input form first.
however, this could be a good opportunity to hone your debugging skills.
what I would normally do in a situation like this is
start at the top of the script as specified in your form’s action attribute and add
echo 'got here'; die();
run your form to check if it gets to your php script
then move the above echo/die down, line by line if you have to, and add appropriate echo statements to display values of variables and then run the form again each time you move the echos.
as part of 3) insert the echos in each part of conditional blocks (IF blocks) to check your code logic is correct
keep doing this until your echos show something is not right. then back track your code to fix the error.
keep repeating 3) and 4) until you get to the end of your script and it works ok.
if you have a debugger, then debugguing will be easier as you can set break points and check values of variables which is essentially what the above steps are doing.
Thanks Kalon. You are very right. My debugging skill are wanting. I have no option than to follow your advice.
I have an eclipse debugger which I use. There is still something I don’t understand which is debugging the form. For example, if I upload a file, how do I handle the debugging between the html form and the script in the debugging environment. I can normally debug a script that has only one entry point. If you understand what I mean, how can I set up the debug to handle the ´html form and the script at the same time. Thanks.
Thanks. I am implementing your steps. So far everything is going ok upto the uploading and processing of uploaded files while using the echo ‘am here’; die(); and some if too. Now I am entering the other part which handles the import. I think your advice is only the way to go.
Finally I got it working. It was all about error handling. There was a segment of the code that got caught in an infinite loop. Thanks Kalon for the hints you gave they were just appropriate for the task.