3 回答

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
我想你想檢查上傳的文件數(shù)量。
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>
試試這個(gè)
<?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 屬性。
動(dòng)作=“上傳.php”
對(duì) PHP 代碼進(jìn)行了細(xì)微的更改以處理上傳。

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊
看來您只是錯(cuò)過了一個(gè)表格action。
它應(yīng)該是這樣的:
<form style="display: none!important;" id="uploadConf" method="post" enctype="multipart/form-data" action="/your/api/call">
或者,您可以使用javascript以下方法為表單附加一個(gè)事件:
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貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
當(dāng)多文件上傳時(shí),您可以使用
$name=$_FILES['file']['name'][$index];
$tmp=$_FILES['file']['tmp_name'][$index];
你的代碼應(yīng)該如下
$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 關(guān)注
- 178 瀏覽
添加回答
舉報(bào)