Form with a "file" type field arrives at the server with "$_POST" empty

Asked

Viewed 1,584 times

8

On our hosting server, I have two projects, where both are with a module in our administrative area to manage images.

In the project To, the form is submitted to the server and the $_POST is received by the server with the image field loaded.

In the proejcto B, the form is submitted to the server but the matrix of $_POST arrives empty to the server, although in Firebug I can see that the field and the loaded image were sent.

Form

Same for project To and B

<form class="horizontal-form" enctype="multipart/form-data" method="post" action="#">
    <div class="row-fluid">
        <div class="span6">
            <div class="control-group">
                <label class="control-label">Carregar imagem</label>
                <div class="controls">
                    <div class="fileupload fileupload-new" data-provides="fileupload">
                        <div class="input-append">
                            <div class="uneditable-input">
                                <i class="icon-file fileupload-exists"></i>
                                <span class="fileupload-preview"></span>
                            </div>
                            <span class="btn btn-file">
                                <span class="fileupload-new">Selecionar ficheiro</span>
                                <span class="fileupload-exists">alterar</span>
                                <input type="file" class="default" name="img">
                            </span>
                            <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">remover</a>
                        </div>
                    </div>
                    <span class="help-block">Escolha uma imagem para substituir a existente no site.</span>
                </div>
            </div>
        </div>
        <div class="span6">
            <div class="control-group">
                <label class="control-label">Pré-visualizar</label>
                <div class="controls">
                    <div class="thumbnail item" style="width:244px">
                        <a href="http://www.meusite.com/caminho/para/imagem/imagem.jpg" title="Pré-visualização da imagem: imagem.jpg" class="fancybox-button">
                            <div class="zoom">
                                <img src="http://www.meusite.com/caminho/para/imagem/imagem.jpg" alt="" />
                                <div class="zoom-icon"></div>
                            </div>
                        </a>
                        <div class="details">
                            imagem.jpg
                        </div>
                    </div>
                    <span class="help-block">Pré-visualizar imagem existente</span>
                </div>
            </div>
        </div>
    </div>
    <div class="form-actions text-right">
        <button type="submit" class="btn blue"><i class="icon-ok"></i> Guardar alterações</button>
        <a class="btn" href="?mod=website&call=images" title="">cancelar</a>
    </div>
</form>

Information from the Firebug

  • Project To

    Captura Tela - Cabeçalhos

    Captura Tela - Post

    Captura Tela - Post Source

  • Project B

    Captura Tela - Cabeçalhos

    Captura Tela - Post

    Captura Tela - Post Source


I have already carried out a series of tests to understand what is going on, some of which may be relevant to finding the solution to this problem:

  1. PHP file with nothing more than the form above.

    In the project To and B the var_dump($_POST); Give me an empty matrix.

  2. Add extra field of type hidden or the type text to the project form B solves the problem and the matrix of $_POST now arriving at the server in condition.

  3. Copy PHP file from project To for the project B to check any problems with the file itself (???), but the result is the same.

  4. File check .htaccess to locate some directive to Apache, but both files are strictly the same and contains no instructions other than:

    # Use PHP5 AS DEFAULT
    AddHandler application/x-httpd-php5 .php
    
    # Frontpage
    # Set the files to be ignored when ussing the directory list
    
    IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
    IndexIgnore *
    
    
    # BROWERS SERVER FILES VIEW DISABLE
    Options -Indexes
    
    
    # CUSTOM ERROR DOCUMENTS FOR APPACHE REDIRECT
    ErrorDocument 400 /error/400.htm
    ErrorDocument 401 /error/401.htm
    ErrorDocument 403 /error/403.htm
    ErrorDocument 404 /error/404.htm
    ErrorDocument 500 /error/500.htm
    
  5. Test on different browsers and different computers to exclude browser or session problems.

    The result was consistent in all tests, project To works well, project B doesn’t work.


Question

Why does the same form work in the project To, but flaw in the project B and tests with a simple form on both domains fail when you would expect that in the project To worked?

  • 1

    Doubt: the statement "In project A and B the var_dump($_POST); gives me an empty matrix." to contradict what you said in the second paragraph. That’s right?

  • Question #2: I noticed that in the images with the source, one has the attribute name worthwhile filename and another with value img. That’s right?

  • 2

    Have you tried comparing the phpinfo() of the two servers? It may be some different configuration.

  • 1

    @utluiz Yes, within the administrative area where the forms are used, in the project To works, in the project B no. But if you take a file with only one form inside and put it in the root of each domain and try it, it doesn’t work in any of them. This is really weird...

  • 1

    @utluiz Yes, project To is named after filename and the project B is named after img, although I have been "playing with" the names to see if it came from there (too short, too long...). This is to test some cause-effect relationship due to the name of input.

  • @bfavaretto As I said, both projects are on the same host server. The output of phpinfo() is strictly the same for all the people present in this machine, including the projects To and B :)

  • According to this topic a possible cause would be to have a general URL rewriting to add "www" to the address of the requests that did not have it. Do you have something like this configured in Apache? I remember that when I configured Virtualhosts in Apache I mapped the two domain types (with and without "www") so I wouldn’t have any problems. Could that be it?

  • And maybe this other link give you some new idea. Also check if your host has specific "php.ini" configuration files by directory. I’ve already used this in some lodgings and can cause confusion.

  • Zuul, when you put an Hidden field the image arrives on the server too? Or will the value of Hidden but not the input file?

  • 1

    Some other ideas: Check if input file is not disabled, check if you are posting for correct action, check the variable $_FILES, and finally the settings of this post: http://stackoverflow.com/a/3587158/664577

  • @Anthonyaccioly With any other input, the entire form arrives at the server and the image is loaded as expected! In short, with another field beyond the file, everything works fine.

  • @Anthonyaccioly Ready, is already solved. This is really something that ready... : D Basically I was checking the $_POST when the form has nothing but $_FILES :) Please add a response warning of this as was the case with your comment so that I can give the matter as resolved. And a big thank you, I was so involved in complex solutions that I didn’t even see the flaw in the verification. (Note: In the project To was checking out the $_FILES, in the project B was checking out the $_POST) And thank you all for your time!

Show 7 more comments

1 answer

5


In accordance with the PHP manual the proper way to handle upload in POST methods is using the global variable $_FILES.

// Uma vez que o nome do seu input file é img
$_FILES['img']['tmp_name']

In the OP case, when the form contained only an input file, only the variable $_FILES was fed. Only by including a input Hidden or something like PHP loaded the data representation of this input into the variable $_POST.


Some other points that may cause problems (not relevant to the OP situation, but that may help other users):

  1. The form shall contain the attribute enctype="multipart/form-data"
  2. The form shall contain the attribute method="POST" (or PUT, but this is a different mammoth)
  3. Make sure you are using double quotes (") in HTML attributes, sometimes you may unintentionally cut and paste smart quotes, angled quotes, etc
  4. Check if a Javasscript function is not disabling the input or form
  5. Check if your input has a name (attribute name). Just the ID is not enough
  6. Make sure that name does not have underscores (_)
  7. Check that two inputs do not have the same name
  8. Check whether the involved directories have read and write permissions
  9. Check whether temporary and destination directories have free disk space
  10. Make sure the form is being properly closed </form>
  11. Check the settings of php.ini; in particular file_uploads, post_max_size and upload_max_file_size
  12. Check the file .htaccess
  13. Check whether your source file no problem for chat start, try to upload small files.

Adapted of that answer user’s shamittomar on Stack Overflow in English.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.