我想追加使用$_POST
方法上載的文件的名稱,從php腳本中發佈$_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']);
?>
哪裏是爲上傳文件形式的代碼? (和任何相關的JS)。 – Quentin