【问题标题】:PHP input type file field being submitted, file isntPHP输入类型文件字段正在提交,文件不是
【发布时间】:2015-09-19 07:12:24
【问题描述】:

我有以下表格。

<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']

这返回真。

但是,文件本身并未发布

$_FILES['custom_pic_upload']

这将返回 false 和

var_dump($_FILES['custom_pic_upload']);

返回空值。

如何让文件本身上传?

【问题讨论】:

    标签: php jquery forms file-upload


    【解决方案1】:

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

    multipart/form-data:用于type属性设置为“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 查找它。

    【讨论】:

    • 好的,我没有那个问题了,但是现在,显然 if (isset($_POST['pic_upload'])) 返回 false
    • 是否正在提交其他 POST 值? print_r($_POST);.
    • 你能发布表单是如何提交的吗?
    • $('.upload_button').click(function(){ var upload_ver_val = $('.picture_upload_input').val(); if (upload_ver_val == '') { alert("请上传图片!"); } else { var ext = $('.picture_upload_input').val().split('.').pop().toLowerCase(); if($.inArray(ext, [' gif','png','jpg','jpeg','svg']) == -1) { alert('只允许扩展名的文件类型;允许 gif、jpg、png 和 svg!'); }否则 { $('.prof_pic_form').submit(); } } });
    • 更新了适用于我的本地代码。哎呀,忘记点击保存了,1秒。
    猜你喜欢
    • 2017-11-22
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    • 2014-12-24
    • 2021-11-03
    • 2015-06-12
    相关资源
    最近更新 更多