dpbl91234 2013-07-24 21:58
浏览 38
已采纳

包括多个文件上传的注释

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>
  • 写回答

1条回答 默认 最新

  • drn1008 2013-07-24 22:10
    关注

    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 ++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行