PHP 正在提交输入类型文件字段,文件不是

PHP input type file field being submitted, file isnt

我有以下表格。

<form action="picture.php" method="POST" class="pic_form">
<span class="picture_box">
<input type="file" accept="image/*" name="pic_upload" class="picture_upload_input" />
<p class="upload_info">Only file types of the extensions gif, jpg, png and svg are allowed. The maximum file size is 15mb. If your file is larger than 15mb, it will not upload.</p>
</span>
<input type="hidden" name="uid" value="<?php echo $_SESSION['userid']; ?>" />
<input type="button" name="upload_button" value="Upload" class="upload_button" />
</form>

我使用 jQuery 验证文件已设置并且类型和大小正确。当我提交此表单(使用 jQuery)时,该字段已发布

$_POST['pic_upload']

这 returns 正确。

但是,文件本身并未发布

$_FILES['custom_pic_upload']

这 returns 错误和

var_dump($_FILES['custom_pic_upload']);

returns 空。

如何让文件自己上传?

您的表单需要属性 enctypehttps://developer.mozilla.org/en-US/docs/Web/HTML/Element/form

multipart/form-data: The value used for an element with the type attribute set to "file".

<form action="picture.php" method="POST" class="pic_form" enctype="multipart/form-data">
<span class="picture_box">
<input type="file" accept="image/*" name="pic_upload" class="picture_upload_input" />
<p class="upload_info">Only file types of the extensions gif, jpg, png and svg are allowed. The maximum file size is 15mb. If your file is larger than 15mb, it will not upload.</p>
</span>
<input type="hidden" name="uid" value="<?php echo $_SESSION['userid']; ?>" />
<input type="submit" name="upload_button" value="Upload" class="upload_button" />
</form>
<script type='text/javascript'> 
$('.upload_button').click(function(){ 
    var upload_ver_val = $('.picture_upload_input').val(); 
    if (upload_ver_val == '') { 
        alert("Please upload an image!");
        return false;
    } else {
        var ext = $('.picture_upload_input').val().split('.').pop().toLowerCase(); 
        if($.inArray(ext, ['gif','png','jpg','jpeg','svg']) == -1) {
            alert('Only file types of the extensions; gif, jpg, png and svg are allowed!');
            return false;
        } else { 
            $('.prof_pic_form').submit(); 
        } 
    } 
});
</script>

关于该主题和教程的其他线程。

What does enctype='multipart/form-data' mean?
http://www.tizag.com/phpT/fileupload.php

此外 PHP 值的名称将是 name 属性,而不是 class,因此请使用 pic_upload.

查找它