3 回答
TA贡献1846条经验 获得超7个赞
我想你想检查上传的文件数量。
HTML
<button class="btn btn-primary btntrg123" id="uploadBtn" style="background: #008489; border-color: #008489">Upload file</button>
<form action="upload.php" 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="0">
<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
$count = 0;
if(isset($_POST['uploadFinish'])) {
$bcode = $_POST['code'];
$newpath = "upload/".$bcode."/";
echo $newpath;
if (!file_exists($newpath)) {
mkdir($newpath, 0755, true);
}
$count = count($_FILES['file']['name']);
if($count < 1) {
$message = "At least 1 file required"; $count='';
} else {
$total = count($_FILES['file']['name']);
for( $i=0 ; $i < $total ; $i++ ) {
move_uploaded_file($_FILES['file']['tmp_name'][$i], $newpath.'/File-'.$bcode.$_FILES['file']['name'][$i]);
}
}
}
?>
为表单添加了 action 属性。
动作=“上传.php”
对 PHP 代码进行了细微的更改以处理上传。
TA贡献1963条经验 获得超6个赞
看来您只是错过了一个表格action。
它应该是这样的:
<form style="display: none!important;" id="uploadConf" method="post" enctype="multipart/form-data" action="/your/api/call">
或者,您可以使用javascript以下方法为表单附加一个事件:
function processForm(e) {
if (e.preventDefault) e.preventDefault();
/* do what you want with the form */
// You must return false to prevent the default form behavior
return false;
}
var form = document.getElementById('uploadConf');
if (form.attachEvent) {
form.attachEvent("submit", processForm);
} else {
form.addEventListener("submit", processForm);
}
TA贡献1785条经验 获得超8个赞
当多文件上传时,您可以使用
$name=$_FILES['file']['name'][$index];
$tmp=$_FILES['file']['tmp_name'][$index];
你的代码应该如下
$total = count($_FILES['file']['name']);
for( $i=0 ; $i < $total ; $i++ ) {
$tmp= $_FILES['file']['tmp_name'][$i];
if (!(empty($tmp )){
$path= "/upload/" . $_FILES['file']['name'][$i];
if(move_uploaded_file($tmp, $path)) {
echo $_FILES['file']['name'][$i]. ' uploaded';
}
}
}
- 3 回答
- 0 关注
- 153 浏览
添加回答
举报