tags:

views:

38

answers:

1

Hello guys, I have searched the forum but the closest question which is about the control stream did not help or i did not understand so i want to ask a different question.

I have an html form which uploads multiples files to a directory. The upload manager that handles the upload resides in the same script with a different code which I need to pass the file names to for processing.

The problem is that the files get uploaded but they dont get processed by the the other code. I am not sure about the right way to pass the $_FILES['uploadedFile']['tmp_name']) in the adjoining code so the files can be processed with the remaining code. Please find below the script.

More specif explanation: this script does specifically 2 things. the first part handles file uploads and the scecond part starting from the italised comment extracts data from the numerous uploaded files. This part has a variable $_infile which is array which is suppose to get the uploaded files. I need to pass the files into this array. so far i struggled and did this: $inFiles = ($_FILES['uploadedFile']['tmp_name']); which is not working. You can see it also in the full code sample below. there is no error but the files are not passed and they are not processed after uploading.

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']);
$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 w*riting 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); }

?>

Please I would appreciate some help. Many thanks

A: 

Try this upload class instead and see if it helps:

To use it simply Upload::files('/to/this/directory/'); It returns an array of file names that where uploaded. (it may rename the file if it already exists in the upload directory)

class Upload {
    public static function file($file, $directory) {
        if (!is_dir($directory)) {
            if (!@mkdir($directory)) {
                throw new Exception('Upload directory does not exists and could not be created');
            }
            if (!@chmod($directory, 0777)) {
                throw new Exception('Could not modify upload directory permissions');
            }
        }

        if ($file['error'] != 0) {
            throw new Exception('Error uploading file: '.$file['error']);
        }

        $file_name = $directory.$file['name'];

        $i = 2;
        while (file_exists($file_name)) {
            $parts = explode('.', $file['name']);
            $parts[0] .= '('.$i.')';
            $new_file_name = $directory.implode('.', $parts);
            if (!file_exists($new_file_name)) {
                $file_name = $new_file_name;
            }
            $i++;
        }

        if (!@move_uploaded_file($file['tmp_name'], $file_name)) {
            throw new Exception('Could not move uploaded file ('.$file['tmp_name'].') to: '.$file_name);
        }

        if (!@chmod($file_name, 0777)) {
            throw new Exception('Could not modify uploaded file ('.$file_name.') permissions');
        }

        return $file_name;
    }

    public static function files($directory) {
        if (sizeof($_FILES) > 0) {
            $uploads = array();
            foreach ($_FILES as $file) {
                if (!is_uploaded_file($file['tmp_name'])) {
                    continue;
                }

                $file_name = static::file($file, $directory);

                array_push($uploads, $file_name);
            }
            return $uploads;
        }
        return null;
    }
}
Petah
thanks for your reply but i am a newbie sorry but i can you explain me more on how to use this function in relation to my problem it is complicated. i dont understand it. when the file are uploaded i need to pass them into a variable like this; $inFiles = ($_FILES['uploadedFile']['tmp_name']). is this correct?
ibiangalex
To read the files you would `$in_files = Upload::files('/to/this/directory/'); if ($in_files) foreach ($in_files as $file) { /* Read $file here */ }`
Petah
Can i do this: $in_files = Upload::files('/to/this/directory/'); if ($in_files) foreach ($in_files as $file) { $numLines = 1; $inFiles = fopen($inFileName,"r"); while(!feof($inFiles)) { $line = fgets($inFiles); $lineTokens = explode(',',$line); if(in_array($lineTokens[0],$searchCriteria)) { if (fwrite($outFile,$line)===FALSE){ the upload class would be in a different page right?
ibiangalex
please one thing I ask. I have the class in it own page, I dont understand the directory creation. is the class creating the directory? or do i have to add a line like this:$upload_directory = dirname(__file__) . '/Uploads/'; please clarify. thanks
ibiangalex
hi Petah, are you there? i dont understand: ('/to/this/directory/')
ibiangalex
replace `'/to/this/directory/'` with the directory you want to upload your files to.
Petah
Thanks for your reply. I got it working but used a different option but your class would come in handy at another time.
ibiangalex