1

I don't know whats the problem. Im trying to do an image upload script. But the choosen file is always empty(null).

Here is my form:

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

And this is an extraction of my simple 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";
    }
}

And here my ajax form:

$('.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;
});

I looked up into info.php and found out that file_upload is allowed but temp_folder has no value. Is this the reason why my script can't find the tmp_name file? I can't change any values cause my hoster do not allow to make any changes in php.ini (can't even get access to file). If so is there any other way to handle this?

2 Answers 2

1

digital upload of binary data needs to be done via hidden iframe. ajax fails with binary data. you can use ajax to poll status

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you now I know where I need to do researches :)
You’re welcome. i discovered this flaw whilst making an ajax attachment function for articles. hopefully in the future ajax will support diffetent mime types.
1

Do a var_dump($_FILES['image']). Part of that output will have an ['error'] parameter. If it's anything OTHER than 0, there was a problem, and you can find out from the list of defined error codes.

Don't check for the presence of the tmp_name. There's cases where an upload fails, but still has a tmp_name. Always check the error code.

4 Comments

Output of var_dump($_FILES['image']); is null too.
if (isset($_POST['coffee_talk_submit'])) { will always be false. CSS class names are NOT submitted with a form. your input button therefore has no name.
Sry forgot to mention that this is an ajax form. Integrated the Javascript into my first post. The form is working for all elements except the upload input
That explains it - you can't do file uploads via AJAX. The standard workaround is to build a hidden iframe, copy the form (plus file input) into the iframe, and submit the form from there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.