简体   繁体   English

将上传的文件传递到脚本的另一部分以进行后续处理

[英]Passing uploaded files to another part of the script for onward processing

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. 我有一个html表单,可将​​多个文件上传到目录。 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 don't 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. 我不确定在相邻代码中传递$_FILES['uploadedFile']['tmp_name'])的正确方法,以便可以使用其余代码来处理文件。 Please find below the script. 请在脚本下面找到。

More specif explanation: 详细说明:

this script does specifically 2 things. 这个脚本专门做两件事。 the first part handles file uploads and the second 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. 这部分有一个变量$_infile ,它是一个数组,用于获取上载的文件。 I need to pass the files into this array. 我需要将文件传递到此数组。 so far I struggled and did this: $inFiles = ($_FILES['uploadedFile']['tmp_name']); 到目前为止,我一直在努力并做到这一点: $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. 没有错误,但文件未通过且上传后未进行处理。

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

?>

Try this upload class instead and see if it helps: 请尝试使用此上载类,看看是否有帮助:

To use it simply Upload::files('/to/this/directory/'); 要使用它,只需上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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM