我得到一個錯誤,指出fileImage在這一行上是一個未定義的索引:$ idx = count($ _ POST ['fileImage'])-1;。現在發生這種情況的原因是,當用戶打開頁面時,顯然沒有任何內容發佈到fileImage,那麼如何在沒有任何內容發佈到$ _POST時將其設置爲「」?我以爲我在下面一行中做了它,但似乎並沒有發生。如何擺脫下面的undefined索引?
下面是代碼:
<?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>
請告訴我有_POST,$ _SESSION之間的空間了[ 'fileImage']? – fhugas 2012-04-28 22:31:48