PHP 文件上传表单提供空值


php file upload form delivers null

我不知道有什么问题。我正在尝试执行图像上传脚本。但是选择的文件始终为空(null)。

这是我的表格:

<form id="coffee_talk_add" action="include/scripts/add_event.php" method="post" accept-charset="utf-8" enctype="multipart/form-data">
    <table>
        <tr>
            <td class="event_width">Datum:</td>
            <td>
                <select name="day_from" id="day_from_e">
                    <option value="none" class="bold italic">Tag</option>
                        <?php
                            for($i=1; $i<=31; $i++){
                                echo "<option value='"".$i."'">".$i."</option>'n";
                            }
                        ?>
                </select>
                <select name="month_from" id="month_from_e">
                    <option value="none" class="bold italic">Monat</option>
                        <?php
                            for($i=1; $i<=12; $i++){
                                echo "<option value='"".$i."'">".$month_name[$i]."</option>'n";
                            }
                        ?>
                </select>
                <select name="year_from" id="year_from_e">
                    <option value="none" class="bold italic">Jahr</option>
                        <?php
                            for($i=2008; $i<=$year; $i++){
                                echo "<option value='"".$i."'">".$i."</option>'n";
                            }
                        ?>
                </select>
            </td>
        </tr>
        <tr>
            <td>Thema:</td>
            <td class="topic"><input type="text" name="topic" id="topic_e" /></td>
        </tr>
        <tr>
            <td>Referent:</td>
            <td class="contributer"><input type="text" name="contributer" id="contributer_e" /></td>
        </tr>
        <tr>
            <td>Beginn:</td>
            <td class="begin"><input type="text" name="begin_hour" id="begin_hour_e" />:<input type="text" name="begin_min" id="begin_min_e" /> Uhr</td>
        </tr>
        <tr>
            <td>Ort:</td>
            <td class="place"><input type="text" name="place" id="place_e" /></td>
        </tr>
        <tr>
            <td>Eintritt:</td>
            <td class="entrance"><input type="text" name="entrance_euro" id="entrance_euro_e" />,<input type="text" name="entrance_cent" id="entrance_cent_e" /> €</td>
        </tr>
        <tr>
            <td>Flyer:</td>
            <td class="flyer">
                <input type="hidden" name="MAX_FILE_SIZE" value="5734400">
                <input type="file" name="image" id="image">
            </td>
        </tr>
    </table>
    <input type="hidden" name="coffee_talk_submit" value="true" />​​​​​​​​​​​​​​​​​
    <div id="add_coffee_talk">
        <input type="submit" id="small" class="coffee_talk_submit" value="speichern">
    </div>
</form>

这是对我简单add_event.php的摘录:

if (isset($_POST['coffee_talk_submit'])) {
    $file = $_FILES['image']['tmp_name'];
    echo var_dump($file);
    if (!isset($file)) {
        //No picture choosen
        echo "No file choosen";
    } else {
        //Do stuff
        echo "upload stuff";
    }
}

这是我的 ajax 形式:

$('.coffee_talk_submit').click(function(){
    if ($('#year_from_e').val() == 'none' || $('#month_from_e').val() == 'none' || $('#day_from_e').val() == 'none' 
            || $('#topic_e').val() == '' || $('#contributer_e').val() == '' || $('#begin_hour_e').val() == '' 
            || $('#begin_min_e').val() == '' || $('#place_e').val() == '' || $('#entrance_euro_e').val() == '' || $('#entrance_cent_e').val() == '') {
        $("#dialog_empty").dialog( "open" );
        return false;
    }
    var form = $('#coffee_talk_add');  
    var data = form.serialize(); 
    $.ajax({
        url: "include/scripts/add_event.php",
        type: "POST",
        data: data,
        dataType: 'json',
        success: function (reqCode) {
            if (reqCode['error_code'] == 1) {
                //Termin erfolgreich eingetragen
                $("#dialog_ok").dialog( "open" );
            } else if (reqCode['error_code'] == 2) {
                //Eintrag bereits vorhanden
                $("#dialog_error").dialog( "open" );
            }
            clear_form_elements(form);
        }
    });
    return false;
});

我查找了信息.php发现file_upload是允许的,但temp_folder没有价值。这是我的脚本找不到tmp_name文件的原因吗?我无法更改任何值,因为我的主机不允许在 php 中进行任何更改.ini(甚至无法访问文件)。如果是这样,还有其他方法可以解决这个问题吗?

二进制数据的数字上传需要通过隐藏的iframe完成。Ajax 在处理二进制数据时失败。您可以使用 AJAX 轮询状态

做一个var_dump($_FILES['image'])。该输出的一部分将具有['error']参数。如果不是 0 ,则存在问题,您可以从定义的错误代码列表中找到答案。

不要检查tmp_name是否存在。在某些情况下,上传失败,但仍有tmp_name。始终检查错误代码。