【发布时间】:2019-09-06 13:41:50
【问题描述】:
PHP 上传:
注意:未定义索引:文件在
在线:$count = count($_FILES['file']['name']);
运行我发现的几乎所有出现此错误的代码...仍然没有任何结果
PHP 代码:
<?php
$count = 0;
if(isset($_POST['uploadFinish'])) {
$bcode = $_POST['code'];
$newpath = "upload/".$bcode."/";
if (!file_exists($newpath)) {
mkdir($newpath, 0755, true);
}
$count = count($_FILES['file']['name']);
if($count < 1) {
$message = "At least 1 file required"; $count='';
} else {
move_uploaded_file($_FILES['file']['tmp_name'], $newpath.'/File-'.$bcode);
}
}
?>
HTML/JS:
<button class="btn btn-primary btntrg123" id="uploadBtn" style="background: #008489; border-color: #008489">Upload file</button>
<form style="display: none!important;" id="uploadConf" method="post" enctype="multipart/form-data">
<div style='height: 0px;width:0px; overflow:hidden;'>
<input type="file" id="file" name="file[]" multiple="multiple" onclick="getFile()" class="btn btn-primary inptri123">
</div>
<input type="hidden" name="code" value="<?php echo $code; ?>">
<input name="uploadFinish" value="1" type="hidden">
</form>
<script type="text/javascript">
$(".btntrg123").click(function(event){
event.preventDefault();
$(".inptri123").trigger('click');
});
function getFile(){
document.getElementById("file").onchange = function () {
var file = this.value;
console.log(file);
var form = document.getElementById('uploadConf');
form.submit();
};
}
</script>
两个代码都是相同的 php 文件。从其他文件运行 php,结果相同。
Console.log 给了我文件,但它没有上传到服务器。 文件夹已创建。
【问题讨论】:
-
$_FILES['file']将是数组 -
小点如果你的代码可读(格式很好),那么它也有可能是可调试的
-
var_dump($_FILES);检查它实际包含的内容。 -
var_dump($_FILES); => 数组(0) { }
-
我在您的表单中没有看到任何 ajax 请求或
action。
标签: javascript php html forms