我收到一条错误,指出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>
答案 0 :(得分:2)
我不知道你在哪里检查,但是这样做的方法是:
if(!isset($_POST['fileImage'))
// Don't do something with the image
else
// Totally do it
答案 1 :(得分:2)
对于上传的文件,请参阅$ _FILES而不是$ _POST。
答案 2 :(得分:0)
如果您不想一直编写日志isset
,可以使用此功能......
function __R($var) {
return isset ( $_REQUEST [$var] ) ? $_REQUEST [$var] : null;
}
$idx = count ( __R ( "fileImage" ) ) - 1;