简体   繁体   English

如何附加上载的文件名?

[英]How to append an uploaded file name?

I have a problem appending the imageNameArray where it is suppose to display file names which have been uploaded. 我在将imageNameArray附加到显示要上传的文件名的地方时遇到问题。

The problem is that lets say I previously uploaded 3 files (cat.png, dog.png and pig.png), when I refresh page and upload another file (panda.png), then when I upload the file, it should display 'panda.png'. 问题是,假设我以前上传了3个文件(cat.png,dog.png和pig.png),当我刷新页面并上传另一个文件(panda.png)时,然后在我上传文件时,它应该显示“ panda.png”。 But instead it is just appending the name of the previously uploaded file (pig.png) and it does not append panda.png. 但是,它只是附加先前上传的文件的名称(pig.png),而不附加panda.png。

IT DISPLAYS: 它显示:

pig.png

IT SHOULD BE: 它应该是:

panda.png

If I upload another file (not refreshing page) such as monkey.png, then again it appends pig.png. 如果我上载另一个文件(而不是刷新页面),例如monkey.png,则它将再次附加pig.png。 No monkey.png. 没有Monkey.png。

IT DISPLAYS: 它显示:

pig.png
pig.png

IT SHOULD BE: 它应该是:

 panda.png
 monkey.png

How can the code below be fixed so that it appends the file names as it should do above? 如何固定下面的代码,以便像上面那样附加文件名?

Below is the javascript code where the appending occurs: 以下是发生附加的javascript代码:

    <?php
    session_start();

    $idx = count($_POST ['fileImage']) -1 ;
    $output = isset($_POST ['fileImage'][$idx]) ? $_SESSION ['fileImage'][$idx]['name'] : "";

    ?>

              function stopImageUpload(success){

              var imageNameArray = ['<?php echo $output ?>'];
              var result = '';



              if (success == 1){
                 result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';

                    for(var i=0;i<imageNameArray.length;i++)
            {
                 $('.listImage').append(imageNameArray[i]+ '<br/>');

             }

              }
              else {
                 result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
              }

        return true;

        }

Below is the php script where it uploads a file which is on another page from the javascript function above: 以下是php脚本,该脚本在其中上传文件,该文件位于上述javascript函数的另一页上:

        <?php

            session_start();

            $result = 0;
            $errors = array ();
            $dirImage = "ImageFiles/";


        if (isset ( $_FILES ['fileImage'] ) && $_FILES ["fileImage"] ["error"] == UPLOAD_ERR_OK) {

        $fileName = $_FILES ['fileImage'] ['name'];

        $fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
        $fileExt = strtolower ( $fileExt );


        $fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName;

                if (count ( $errors ) == 0) {
                    if (move_uploaded_file ( $fileTemp, $fileDst )) {
                        $result = 1;


                    }
                }

            }

    $_POST ['fileImage'][] = array('name' => $_FILES ['fileImage']['name']);

            ?>

        <script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result;?>);</script>

why you useing Session? 为什么使用Session? try to add session_destroy(); 尝试添加session_destroy(); in the end of the second script and the first one. 在第二个脚本和第一个脚本的末尾。 I think its should delete all the previos data and then you know more whats the problem. 我认为它应该删除所有previos数据,然后您知道更多问题了。

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

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