-2

我想附加已使用从 php 脚本$_POST发布的方法上传的文件的名称。$_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']);
?>
4

1 回答 1

1

因为您正在寻找$_POST一个位于$_SESSION. 尝试将其更改为:

$output = array();
if(isset($_SESSION['fileImage'])){
    $idx = count($_SESSION['fileImage']) -1 ;
    $output[] = isset($_SESSION['fileImage'][$idx]) ?
        $_SESSION['fileImage'][$idx]['name'] : "";
}
于 2012-04-29T17:24:49.727 回答