gpt4 book ai didi

php - 如何摆脱下面 undefined index ?

转载 作者:行者123 更新时间:2023-11-28 16:19:07 25 4
gpt4 key购买 nike

我收到一条错误,指出 fileImage 是此行中 undefined index : $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>

最佳答案

我不知道你在哪里检查,但这样做的方法是:

if(!isset($_POST['fileImage'))
// Don't do something with the image
else
// Totally do it

关于php - 如何摆脱下面 undefined index ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10368103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com