为什么不设置$ FILE

时间:2014-11-07 11:58:39

标签: php html forms file file-upload

这是我的html表单的片段

<form class="form-horizontal" role="form" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="profilepictureinput">Profile Picture</label>
        <input type="file"
               id="profilepictureinput"
               name="profilepicture">
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

当我发布表格时,php说的是

isset($_FILE['profilepicture']) 

返回false。这是为什么?

3 个答案:

答案 0 :(得分:1)

$_FILE更改为$_FILES

isset($_FILES['profilepicture']) 

如果表单未提交,它将返回false。提交后,它将返回true

答案 1 :(得分:0)

正如Tom Kriek所说,正确的语法是:

$_FILES['profilepicture']

答案 2 :(得分:0)

首先,如果提交了表单,则需要使用$ _POST进行检查,然后才能从$ _FILES获取信息。 你应该这样写:

<form class="form-horizontal" role="form" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="profilepictureinput">Profile Picture</label>
        <input type="file" id="profilepictureinput" name="profilepicture">
    </div>
    <button type="submit" class="btn btn-default" name="new-picture">Submit</button>
</form>

这是改变:

<button type="submit" class="btn btn-default" name="new-picture">Submit</button>

并查看isset($_POST['new-picture']) upload()函数打印出$ _FILES数组的内容,这样就可以看出,问题是什么。

<?php
//$newImageSubmitted is TRUE if form was submitted, otherwise FALSE
$newImageSubmitted = isset( $_POST['new-image'] );

if ( $newImageSubmitted ) {
    //this code runs if form was submitted
    $output = upload();
} else {
    //this runs if form was NOT submitted
    $output = include_once "views/upload-form.php";
}
return $output;

function upload(){
    $out = "<pre>";
    $out .=print_r($_FILES, true);
    $out .= "</pre>";
    return $out;
}