doukui4786 2012-04-24 22:46
浏览 30

它只显示一个文件名

I have a $_SESSION variable which should display names of files which have been uploaded. The problem is that it is not doing this, it is only display the name of a file for 1 file and 1 file only. For example if I upload files 'jellyfish.png' and 'tulips.png', it should display the file names 'jellyfish.png' and 'tulips.png' for $_SESSION['fileImage']; but instead it is displaying both file names as the first name like this: 'jellyfish.png' and 'jellyfish.png'.

When I do a var_dump($_SESSION ['fileImage']); for the example above, it outputs this: string(13) "Jellyfish.jpg", no matter which file is uploaded. If I refresh the browser and lets say the last file I uploaded into the server was 'tulips.png', then it outputs this: string(10) "tulips.jpg", for every file no matter which file is uploaded before the next refresh.

Does anyone know how to fix this so it displays the correct file names for each uploaded file under the $_SESSION variable?

Below is the php script:

<?php

session_start();

$dirImage = "ImageFiles/";

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

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

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

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


    $fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName;
    $filePrifix = basename ( $fileName, "." . $fileExt );
    $i = 0;
    while ( file_exists ( $fileDst ) ) {
        $i ++;
        $fileDst = $dirImage . DIRECTORY_SEPARATOR . $filePrifix . "_" . $i . "." . $fileExt;


    }
    // Move the file

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


        }
    }

}

?>

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

Below is the Javascript code which contains the JSON:

function stopImageUpload(success){

      var imagename = <?php echo json_encode(isset($_SESSION ['fileImage']) ? $_SESSION ['fileImage'] : null); ?>;
      var result = '';
      if (success == 1){
         result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
         $('.listImage').append(imagename + '<br/>');
      }
      else {
         result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
      }

return true;

}

<?php
var_dump($_SESSION ['fileImage']);
?>
  • 写回答

1条回答 默认 最新

  • dongzai2952 2012-04-24 22:56
    关注

    Session is not working because you copied a script written by me and deleted the question, not adding it as a new question for the 3rd time .. if you wrote the script your self you should have been able to correct that simple error

    Let this be the very last time you do such

    Replace

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

    with

     $_SESSION ['fileImage'][] = $_FILES ['fileImage']['name'];
    
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)