未定义的索引 -> PHP 上传不起作用

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 给了我文件,但它没有上传到服务器。文件夹已创建。


繁星点点滴滴
浏览 155回答 3
3回答

喵喵时光机

我想你想检查上传的文件数量。HTML<button class="btn btn-primary btntrg123" id="uploadBtn" style="background: #008489; border-color: #008489">Upload file</button>&nbsp;<form action="upload.php" style="display: none!important;" id="uploadConf" method="post" enctype="multipart/form-data">&nbsp; &nbsp; <div style='height: 0px;width:0px; overflow:hidden;'>&nbsp; &nbsp; &nbsp; &nbsp; <input type="file" id="file" name="file[]" multiple="multiple" onclick="getFile()" class="btn btn-primary inptri123">&nbsp; &nbsp; </div>&nbsp; &nbsp; <input type="hidden" name="code" value="0">&nbsp; &nbsp; <input name="uploadFinish" value="1" type="hidden">&nbsp;</form><script type="text/javascript">$(".btntrg123").click(function(event){&nbsp;&nbsp; &nbsp; event.preventDefault();&nbsp; &nbsp; $(".inptri123").trigger('click');});&nbsp;function getFile(){&nbsp;&nbsp; &nbsp; document.getElementById("file").onchange = function () {&nbsp; &nbsp; &nbsp; &nbsp; var file = this.value;&nbsp; &nbsp; &nbsp; &nbsp; console.log(file);&nbsp; &nbsp; &nbsp; &nbsp; var form = document.getElementById('uploadConf');&nbsp; &nbsp; &nbsp; &nbsp; form.submit();&nbsp; &nbsp; };}</script>试试这个&nbsp; &nbsp; <?php$count = 0;if(isset($_POST['uploadFinish']))&nbsp; {&nbsp;&nbsp; &nbsp; $bcode = $_POST['code'];&nbsp; &nbsp; $newpath = "upload/".$bcode."/";&nbsp; &nbsp; echo $newpath;&nbsp; &nbsp; if (!file_exists($newpath)) {&nbsp; &nbsp; &nbsp; &nbsp; mkdir($newpath, 0755, true);&nbsp; &nbsp; }&nbsp; &nbsp; $count = count($_FILES['file']['name']);&nbsp; &nbsp; if($count < 1) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $message = "At least 1 file required"; $count='';&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $total = count($_FILES['file']['name']);&nbsp; &nbsp; &nbsp; &nbsp; for( $i=0 ; $i < $total ; $i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; move_uploaded_file($_FILES['file']['tmp_name'][$i], $newpath.'/File-'.$bcode.$_FILES['file']['name'][$i]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}?>为表单添加了 action 属性。动作=“上传.php”对 PHP 代码进行了细微的更改以处理上传。

神不在的星期二

看来您只是错过了一个表格action。它应该是这样的:<form style="display: none!important;" id="uploadConf" method="post" enctype="multipart/form-data" action="/your/api/call">或者,您可以使用javascript以下方法为表单附加一个事件:function processForm(e) {&nbsp; &nbsp; if (e.preventDefault) e.preventDefault();&nbsp; &nbsp; /* do what you want with the form */&nbsp; &nbsp; // You must return false to prevent the default form behavior&nbsp; &nbsp; return false;}var form = document.getElementById('uploadConf');if (form.attachEvent) {&nbsp; &nbsp; form.attachEvent("submit", processForm);} else {&nbsp; &nbsp; form.addEventListener("submit", processForm);}

慕的地10843

当多文件上传时,您可以使用$name=$_FILES['file']['name'][$index];$tmp=$_FILES['file']['tmp_name'][$index];你的代码应该如下$total = count($_FILES['file']['name']);for( $i=0 ; $i < $total ; $i++ ) {&nbsp; $tmp= $_FILES['file']['tmp_name'][$i];&nbsp; if (!(empty($tmp )){&nbsp; &nbsp; $path= "/upload/" . $_FILES['file']['name'][$i];&nbsp; &nbsp; if(move_uploaded_file($tmp, $path)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo $_FILES['file']['name'][$i]. ' uploaded';&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP