包括多个文件上传的注释

包括多个文件上传的注释

问题描述:

I have a problem when uploading multiple files. I have a category drop down and a comments box that I need to be inserted into the database along with the file details. I have five upload boxes displayed on the screen.

If I upload one file, the comment is blank (even when it shoudln't be) and the category is always set to the first value. Afte some testing it looks as though the final upload comment and category boxes are overwriting all of the overs. Tested by uploading five files with different categories and comments, all showed the last comment and category.

I can see the problem in the script but I can't figure out how to assign a category or comment to each particular instance of the upload.

Any help is appreciated.

     <?php



    $upload_dir = "training/trainingDocuments/";
    $maxUploads = 5;

    $msg    = "Please select file(s) for uploading";
    $errors = array();

    if ( $files = convert_files( $_FILES["myTrainingFile"] ) ) {
        foreach( $files as $i => $file ) {
            $fileName = $file["name"];
            $tempName = $file["tmp_name"];
            $fileSize = $file["size"];
            $fileExt  = strtolower( pathinfo( $fileName, PATHINFO_EXTENSION ) );
            $blacklist  = array( 'exe','php','jsp','js','bat','asp','aspx','com','dmg' );

//                $newPath = $upload_dir .$fileName;
            $dateUploaded = time() . microtime();

            if ( in_array( $fileExt, $blacklist ) ) {
                $errors[$i] = "File type not allowed";
            }

            if ( !is_uploaded_file( $tempName ) ) {
                //$errors[$i] = "Uploading ".$fileName." failed.";
            }else{
                echo '<h4>Uploading of :'.$fileName.' was a success.</h4>';
            }

            //if(file_exists($newPath)){

                $name = pathinfo($fileName, PATHINFO_FILENAME);
                $fileName = $name . '__' .uniqid().  '.' .$fileExt;
                $newPath = $upload_dir .$fileName;
//                }else{
//
//                }

            if ( isset( $errors[$i] ) ) {
                continue;
            }


            if ( !move_uploaded_file( $tempName, $newPath ) ) {
                //$errors[$i] = "Uploading ".$fileName." failed.";
            }else{
                //echo 'File moved';
            }

            $comments = htmlentities( trim( $_POST['comments'] ) );
            $category = htmlentities( trim( $_POST['category'] ) );

            if($file['name'] != ""){
                $training->uploadDocument( $fileName, $category, $comments );
            }
        }
    }
    ?>

    <?php
    function convert_files( $files ) {
        if ( is_array( $files ) && !empty( $files["name"] ) ) {
            if ( is_array( $files["name"] ) ) {
                $merged = array();
                foreach( $files["name"] as $i => $name ) {
                    $merged[] = array(
                        "name"  =>  $name,
                        "type"  =>  $files["type"][$i],
                        "size"  =>  $files["size"][$i],
                        "error" =>  $files["error"][$i],
                        "tmp_name"  =>  $files["tmp_name"][$i]
                    );
                }
                return $merged;
            }
            return array( $files );
        }
        return false;
    }
    ?>

EDIT: ADDED HTML

 <div id="uploadFormContainer">
    <form id="uploadForm" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post" enctype="multipart/form-data">

        <?php
        $num = 0;
        while($num < $maxUploads)
        {?>
            <div class="uploadFormE">
                <label>File Category: </label>
                <select name="category">
                    <option value="doc">Documents (pages, word, PDF etc)</option>
                    <option value="sheet">Spreadsheet</option>
                    <option value="vid">Video</option>
                    <option value="pres">Presentations</option>
                    <option value="img">Image</option>
                    <option value="quiz">Quiz</option>
                    <option value="other">Other</option>
                </select>
             </div>
             <div class="uploadFormE">
                 <label>File Location: </label>
                 <input type="file" name="myTrainingFile[]" />
             </div>
             <div class="uploadFormE">
                <label>Comments: </label>
                <textarea class="GAH" name="comments"> </textarea>
             </div>
            <hr/>
            <br />
        <?php $num++;
        }
        ?>

        <input type="submit" value="Upload File(s)">
    </form>
    </div>

See if this works. This is for the case when there is only one comment and category:

Take this part:

$comments = htmlentities( trim( $_POST['comments'] ) );
$category = htmlentities( trim( $_POST['category'] ) );

out of the foreach loop:

if ( $files = convert_files( $_FILES["myTrainingFile"] ) ) {
    foreach( $files as $i => $file ) {

Since you are getting only one value for comments and category from the previous page, you don't need this in the foreach loop. Additionally, if you want comments and category for each file uploaded, then you need to make changes to the previous page that is passing values to this page. If you want that then you need to add more code.

These are the updates after OP added more code:

Change this:

<select name="category">

to this:

<select name="category[]">

and this:

<textarea class="GAH" name="comments"> </textarea>

to this:

<textarea class="GAH" name="comments[]"> </textarea>

and then in your original foreach loop, change this:

$comments = htmlentities( trim( $_POST['comments'] ) );
$category = htmlentities( trim( $_POST['category'] ) );

to this:

$comments = htmlentities( trim( $_POST['comments'][$i] ) );
$category = htmlentities( trim( $_POST['category'][$i] ) );

This is based on the assumption that $i is the index variable whose value goes from 0 to 4 as you iterate through the files array. If that is not the case then you need to add another index variable say $count like so:

$count = 0;
foreach( $files as $i => $file ) {
    // your other code goes here
    $comments = htmlentities( trim( $_POST['comments'][$count] ) );
    $category = htmlentities( trim( $_POST['category'][$count] ) );

    $count ++;
}