我想附加已使用从 php 脚本$_POST
发布的方法上传的文件的名称。$_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']);
?>