vendredi 5 juin 2015

PHP "switch" and PHP "if" do NOT work in this code

I'm literally going to smash my computer against the wall. It's running Windows 7 Professional with all the updates, WAMPServer 2.5, with Apache 2.4.9 and PHP 5.5.12.

The following PHP code REFUSES to run with either "if" or "switch" programming. It literally sails right by the program without looking, and doesn't execute steps that depend on either one working.

//  Logs into the database...
require_once("includes/sysAdmin.php");

//  Obtains the temporarily uploaded file name...
$file = isset($_FILES['file']['tmp_name']) ? $_FILES['file']['tmp_name'] : '';
$control = "";
$contents = "";
$sqlHead = "";
$arrPick = 0;
$arrLoop = 0;
$recSend = "";
$recSave = "";
$recPost = "";
$sqlRun = "";
$query = "";
$tally = 0;
$limit = 0;

//  Selects the directory to store the temporarily upload...
$target_path = "uploads/";
//  Sets up the correct file name to allow the file to be moved for storage and processing...
$target_path = $target_path . basename( $_FILES['file']['name']);
$remove = $target_path;

//  Only operates if there is a temporarily uploaded file that can be read...
if (file_exists($file) && is_readable ($file)) {

    //  Only operates if the file can be moved to the target path for storage and processing...
    //  The file gets moved in the process...
    if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {

        //  Indicates success...
        echo "Uploaded the file: <strong>" . basename( $_FILES['file']['name']). 
            "</strong>...<br />";

        //  Opens the file for processing...
        $file_handle = fopen($target_path, "r");
        $split = fgets($file_handle);

        echo $split . " is the dividing variable between sections.<br />";
        //  Sets up loop progress...
        $loop = 0;
        //  Reads the entire file into a string...
        while (!feof($file_handle)) {
            //  Adds more data to the input string...
            $contents .= fread($file_handle, 8192);
            //  Tracks loop progress...
            $loop = $loop + 1;
        }

        //  Stops using the file...
        fclose($file_handle);

        //  Converts the input string into an array...
        $pieces = explode($split, $contents);
        //  Obtains the number of array segments found...
        $units = count($pieces);
        //  Reports that number...
        echo "Imported " . $units . " data updates.<br />";
        //  Resets loop tracking...
        $loop = 0;
        //  Resets the file path to make importing easier...
        $target_path = "uploads/import-cache.txt";

        //  Loops through array segments...
        foreach($pieces as $value) {
            //  Tracks looping through the array segments...
            $loop = $loop + 1;
            //  Starts using the file for temporary data storage...
            $file_handle = fopen($target_path, 'w');
            //  Temporarily stores an array segment in the file...
            fwrite($file_handle, $value);
            //  Saves the file to allow information to be retrieved from it...
            fclose($file_handle);
            //  Converts the current array segment into another array...
            $array = explode(",", $value);
            //  Attempts to get the table setting...
            $control = (string)$array[0];
            $limit = $array[1];
            echo "The " . $control . " section submits " . $limit . " records at a time.<br />";
            //  Gets rid of the array segment...
            $array = "";
            //  Uses the table setting to control data import queries...
            $array = file($target_path);

            //  HERE LIES THE REGION WHERE THIS CODE CANNOT INTERPRET DATA.
            //  IT HAS BECOME IMPOSSIBLE TO TEST A VARIABLE TO SEE WHAT ITS
            //  CONTENT IS, THEREBY PREVENTING FURTHER PROGRESS.

            //  Imports the temporary file as an array of individual records...
            foreach($array as $record){

                //  Tracks progress through the records...
                $tally = $tally + 1;

                echo $control . ":  " . $record . "<br /><br />";

                if ($tally < $limit) {
                    //  Formats data for insertion...
                    $recSend = "(" . $record . "), ";
                    //  Transfers formatted data to the buffer...
                    $recPost = $recPost . $recSend;
                    //  Prevents duplication...
                    $recSend = "";
                }
                if ($tally == $limit) {
                    //  Formats the final string for insertion...
                    $recSend = "(" . $record . ");";
                    //  Forms the entire APPEND query...
                    $recSave = $sqlHead . $recSave . $recSend;
                    //  Normally, the query would run, but for now,
                    //  just PRINT THE DATA ALREADY!!!!!!!
                    //echo $recSave . "<br /><br />";
                    //  Processes data by submitting the query...

                }

            }

        }
    } else{
        //  The file didn't load properly...
        echo "There was an error uploading the file, please try again!<br />";
    }
} else{
    //  There is no file to upload...
    echo "You did not specify a file to upload.";
}
?>

The stupidest part of it all?!?

It still executing "if" statements farther down the page!!!

I swear I'm going to smash my computer into tiny little bits! What the heck is wrong with this thing? Did PHP decide to deprecate code on Fridays, when I have projects that need finishing?!?

Aucun commentaire:

Enregistrer un commentaire