I can’t get up to the server

Asked

Viewed 34 times

0

I am trying to upload a file to the server (PDF) and it does not go up. Na page that is to send the file, when I click to send nothing happens.

Code of the page where the file is sent:

<?php
require_once("elementos.php");
?>
                <div class="alinha">
                <h3>Licitações</h3>
                <form action="cadastralicitacao.php" method="post">
                  <div class="form-group">
                    <label for="exampleInputEmail1">Objeto</label>
                    <textarea class="form-control" name="objeto" rows="4"></textarea>
                  </div>
                  <div class="form-group">
                    <label for="exampleInputPassword1">Modalidade</label>
                    <select class="form-control" name="modalidade">
                      <option value="Pregão">Pregão</option>
                      <option value="Concorrência">Concorrência</option>
                      <option value="Carta convite">Carta convite</option>
                      <option value="Tomada de preços">Tomada de preço</option>
                      <option value="Leilão">Leilão</option>
                      <option value="Chamamento">Chamamento</option>
                      <option value="Dispensa">Dispensa</option>
                      <option value="Inexigibilidade">Inexigibilidade</option>
                    </select>
                  </div>
                  <div class="form-group">
                    <label for="exampleInputFile">Status do processo</label>
                    <select class="form-control" name="status">
                      <option value="Em andamento">Em andamento</option>
                      <option value="Encerrado">Encerrado</option>
                    </select>
                  </div>
                  <div class="form-group">
                    <label>Número do processo</label>
                      <input type="text" name="numprocesso" class="form-control" placeholder="Forneça o número do processo">
                  </div>
                  <div class="form-group">
                    <label>Data de abertura</label>
                    <input type="date" class="form-control" name="dataabertura" placeholder="Forneça o número do processo">
                  </div>
                  <div class="form-group">
                    <form method="post" action="recebe_upload.php" enctype="application/x-www-form-urlencoded">
                      <input type="hidden" name="enviou" value="1">
                      <label>Arquivo</label>
                      <input type="file" name="arquivo" />
                    </form>
                  </div>
                  <button type="submit" class="btn btn-default">Enviar</button>
                </form>
                </div>
            </div>
        </div>
 <?php
 require_once("rodape.php");

Code in PHP:

<?php 
require_once "funcoes.php";

$msg = false;

if( isset($_POST['enviou']) && $_POST['enviou'] == 1 ){

    // arquivo
    $arquivo = $_FILES['arquivo'];

    // Tamanho máximo do arquivo (em Bytes)
    $tamanhoPermitido = 1024 * 1024 * 2; // 2Mb

    //Define o diretorio para onde enviaremos o arquivo
    $diretorio = "uploads/";

    // verifica se arquivo foi enviado e sem erros
    if( $arquivo['error'] == UPLOAD_ERR_OK ){

        // pego a extensão do arquivo
        $extensao = extensao($arquivo['name']);

        // valida a extensão
        if( in_array( $extensao, array("pdf") ) ){

            // verifica tamanho do arquivo
            if ( $arquivo['size'] > $tamanhoPermitido ){

                $msg = "<strong>Aviso!</strong> O arquivo enviado é muito grande, envie arquivos de até ".$tamanhoPermitido/MB." MB.";
                $class = "alert-warning";

            }else{

                // atribui novo nome ao arquivo
                $novo_nome  = md5(time()).".".$extensao;

                // faz o upload
                $enviou = move_uploaded_file($_FILES['arquivo']['tmp_name'], $diretorio.$novo_nome);

                if($enviou){
                    $msg = "<strong>Sucesso!</strong> Arquivo enviado corretamente.";
                    $class = "alert-success";
                }else{
                    $msg = "<strong>Erro!</strong> Falha ao enviar o arquivo.";
                    $class = "alert-danger";
                }
            }

        }else{
            $msg = "<strong>Erro!</strong> Somente arquivos PDF são permitidos.";
            $class = "alert-danger";
        }

    }else{
        $msg = "<strong>Atenção!</strong> Você deve enviar um arquivo.";
        $class = "alert-info";
    }
}
?>
  • But there is some error message from the ones you used in Ifs or you get a "blank" screen or some HTTP error?

  • No, nothing happens, that’s the problem

  • Nothing happens is very vague, when you click on the button for some page it should go right? of course you’re not able to explain right, anyway I’ve found the problem, look at the answer.

  • 1

    It excludes my answer, after so many editions, only now I saw the complete code and the two Forms, and when I answered Guilherme replied together.

  • 1

    @Fernandovr because answering vague questions gives headaches, the idea is to guide the author to read the MCVE: https://answall.com/help/mcve - so avoid these problems.

  • It is true @Guilhermenascimento, it is q to me the first issue of his question seemed a simple problem. In this case it wasn’t even that it was vacant, but he posted his code incomplete. kkk But it catches nothing.

Show 1 more comment

1 answer

3

The problem is that FORM can’t go in form:

        <!-- form fora --->
    <form action="cadastralicitacao.php" method="post">
      ...
      <div class="form-group">

        <!-- form dentro --->
        <form method="post" action="recebe_upload.php" enctype="application/x-www-form-urlencoded">
          <input type="hidden" name="enviou" value="1">
          <label>Arquivo</label>
          <input type="file" name="arquivo" />
        </form>
        <!-- form dentro --->

      </div>
      <button type="submit" class="btn btn-default">Enviar</button>
    </form>
        <!-- form fora --->

  </div>

That’s if you do it:

<form>
    <form>
    </form>
</form>

It won’t work, but if you do it will:

<form>
</form>

<form>
</form>

Take the form from inside the other and one more detail, the upload form cannot use "application/x-www-form-urlencoded", he must use enctype="multipart/form-data"

  • This worked, however the file is not appearing in the folder, it does not go up

  • @Pedroribeiro is but which if it falls when you complete the upload? It falls on Sucesso!?

  • }Else{ // assigns new name to the file $new_name = md5(time().". $extension; // upload $sent = move_uploaded_file($_FILES['file']['tmp_name'], $directory.$new_name); if($sent){ $msg = "<Strong>Success! </Strong> File sent correctly." ; $class = "Alert-Success";

  • @Pedroribeiro I’m not asking for code, I’m asking which if "he enters" when you send the POST.

  • Maybe you have a problem with the function he is using to get the file extension. I think q if he changes the line: $extensao = extensao($arquivo['name']); for $extensao = pathinfo($arquivo['name'], PATHINFO_EXTENSION); can solve one of the problems. And tbm is necessary to check if the upload folder exists and if the path is correct, if it will not give error tbm.

  • Parse error: syntax error, Unexpected '$msg' (T_VARIABLE) in C: xampp htdocs painelnovo pages recebe_upload.php on line 5

  • This error I fixed, now it is accusing error in this line: $file = $_FILES['file']; Notice: Undefined index: file in C: xampp htdocs painelnew pages received_upload.php on line 10

  • worked right here already, thanks to everyone who helped me

Show 3 more comments

Browser other questions tagged

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