0

我收到一条错误消息,指出 fileImage 是此行上的未定义索引:$idx = count($_POST ['fileImage']) -1 ;。现在发生这种情况是因为当用户打开页面时,显然没有任何内容发布到 fileImage,那么当没有任何内容发布到 $_POST 时,如何将其设置为“”?我以为我在下面的行中做到了,但似乎没有发生。

下面是代码:

<?php
session_start();

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

?>

Javascript:

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;
}​

下面是 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>
4

3 回答 3

2

我看不到您在哪里检查,但这样做的方法是:

if(!isset($_POST['fileImage'))
      // Don't do something with the image
else
      // Totally do it
于 2012-04-28T22:28:52.357 回答
2

对于上传的文件,请参考 $_FILES 而不是 $_POST。

http://php.net/manual/en/reserved.variables.files.php

于 2012-04-28T22:31:41.847 回答
0

如果您不想一直写日志isset,可以使用此功能...

function __R($var) {
    return isset ( $_REQUEST [$var] ) ? $_REQUEST [$var] : null;
}

$idx = count ( __R ( "fileImage" ) ) - 1;
于 2012-04-28T22:33:00.607 回答