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

7

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

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

In the B project, the form is submitted to the server but the $_POST array arrives empty to the server, although in Firebug I can see that the field and the loaded image have been sent.

Form

Same for project A 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>

Firebug Information

  • Project A

  • Project B

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

  • PHP file with nothing more than the form above.

    In the A and B project, the var_dump($_POST); gives me an empty array.

  • Adding extra field of type hidden or type text to project form B solves the problem and the array of $_POST starts arriving at the server in conditions.

  • Copy PHP file from the A project to the B project to check for any issues with the file itself. ), but the result is the same.

  • Verifying the .htaccess files to find some Apache directives, but both files are strictly the same and contain 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
    
  • Test in different browsers and different computers to exclude problems caused by browser or sessions.

    The result was consistent across all tests, project A works fine, project B does not work.

  • Question

    Why does the same form work in the A project, but fails in the B project, and tests with a simple form in both domains fail when you expect it in the project A work?

        
    asked by anonymous 09.01.2014 / 16:32

    1 answer

    5

    As the PHP manual the proper way to handle files of upload in POST methods is by using the global variable $ _FILES .

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

    In the case of the OP, when the form contained only one input file, only the $_FILES variable was fed. Just by including an input or something like that, PHP would load the data representation of that input into the variable $_POST .

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

  • The form must contain the enctype="multipart/form-data"
  • The form must contain the method="POST" attribute (or PUT , but this is a different mammoth)
  • Make sure you're using double quotes ( " ) in HTML attributes, sometimes you may inadvertently cut and paste smart quotes, angle quotes, etc.
  • Check if a JavasSript function is not disabling input or form
  • Verify that your input has a name (attribute name ). Only ID is not enough
  • Verify that this name does not have underscores ( _ )
  • Verify that two inputs do not have the same name
  • Verify that the directories involved have read and write permissions
  • Verify that temporary and target directories have Free disk space
  • Verify that the form is being properly closed </form>
  • Check your php.ini settings; in particular file_uploads , post_max_size and upload_max_file_size
  • Check the file .htaccess
  • Make sure your source file does not have a problem to start with, try uploading small files.
  • Adapted that answer shamittomar on Stack Overflow in English.

        
    09.01.2014 / 19:27