它不附加变量

时间:2012-04-29 17:17:04

标签: php javascript

我想附加使用$_POST方法上传的文件的名称,以便从php脚本发布$_FILES['fileImage']['name']。问题是文件上传后,我没有看到附加的文件名,只是显示一个空白。为什么在成功上传文件后没有附加文件的名称?

如果有人能提供编码示例,那将非常有帮助。

以下是Javascript代码:

   <?php
    session_start();

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

    ?>

     <script>   


    function stopImageUpload(success) {
        var imageNameArray = <?php echo  json_encode($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;
    }

</script>

下面是上传文件的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;
        }
    }
}

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

1 个答案:

答案 0 :(得分:1)

因为您在$_POST查找$_SESSION中的变量。尝试将其更改为:

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