File upload does not send

Asked

Viewed 76 times

3

I’m trying to upload the file, but it doesn’t send to the folder. Shows the entire file sending process, receives in the database the new file name, but the file does not arrive in the server folder.

<?php
if ((isset($_POST["form"])) && ($_POST['form'] == "form1")) {

  $ext = strtolower(substr($_FILES['arquivo']['name'],-4)); //Pegando extensão do arquivo
  $arquivo = "file_".date('dmYhis') . $ext; //Definindo um novo nome para o arquivo
  $dir = __DIR__.'/uploads/aulas/'; //Diretório para uploads
  move_uploaded_file($_FILES['arquivo']['tmp_name'], $dir.$arquivo); //Fazer upload do arquivo

}
?>


  <form id="form" method="post" autocomplete="off" enctype="multipart/form-data">
    <div class="row">
      <div class="col-xs-6">
        <div class="form-group">
          <label class="control-label">Arquivo (.zip .rar .pdf)</label>
          <input name="arquivo" type="file" id="file-1" required />
        </div>
      </div>

      <button type="submit" id="button" name="button" class="btn btn-success pull-right"><i class="icon-save bigger-110"></i> Salvar</button>
  </form>

UPDATE 1:

I ran the test on another server and it worked. I wonder what?

Update 2:

By placing a echo '<pre>';print_r($_FILES);echo '</pre>'; to see the exit, and it looks like this:

Array
(
    [arquivo] => Array
        (
            [name] => artes-i---eja.pdf
            [type] => 
            [tmp_name] => 
            [error] => 1
            [size] => 0
        )

)
  • Already checked the folder permissions?

  • @Andersoncarloswoss Yes

  • Do me a favor. I know people don’t really like to do this in php but put your code inside try to see if you can extract some information.

  • I never used the try, how should I do?

  • https://www.php.net/manual/en/language.exceptions.php

  • @Augustovasques I did and returned no value.

  • @Augustovasques looks like it’s something on the server, something you’re blocking, because on the cPanel server it works on another server that uses another panel, it doesn’t work.

  • 2

    Now that I’ve seen [error] => 1, your file exceeded the size set in the directive upload_max_filesize within the php.ini

  • @Augustovasques but in my php.ini file this with 12M upload_max_filesize=12M. This php.ini is in the root folder public_html/, where the action is happening is inside the folder public_html/sistema/ , I need to put another php.ini inside the system folder?

  • This is not the conventional way to php.ini.

  • Hosting allows you to create custom php.ini

  • 1

    Custom does not mean that you recognize all fields. I believe it is an internal size limit of them, get in touch with the support and pass this information to them.

  • 1

    @Augustovasques misses exactly that. Give your answer to I put score for you. Thanks!

  • 1

    Thank you, just helping out, I’m glad. Now if you were grateful to enter my profile and read the answers I wrote and if any you find useful and want to score I will be flattered.

Show 9 more comments

1 answer

1

Check the following PHP settings directives (php.ini):

  • upload_max_filesize - Sets the maximum file size in a POST.
  • post_max_size - Sets the maximum post size (including the file size).

Check if the first php file that is accessed by the user / webserver has the necessary permissions in the upload folder.

In addition I recommend that you make some changes to avoid future problems when using your script:

<?php
function uploadFile($file){

$resultado['ok'] = true;
$formatos = ['pdf', 'zip', 'rar'];
$tamanhoMaximo = 10 * (1024*1024); //10MB
$ds = DIRECTORY_SEPARATOR;
//Contem o caractere usado no endereço do arquivo para separar os diretórios no SO atual

$dir = __DIR__."{$ds}uploads{$ds}aulas{$ds}";


$x = explode('.', $file['name']); 
//Quebra a string a cada . e retorna um array
//documento.pdf
//['documento', 'pdf']

$ext = strtolower(end($x)); //Retorna o ultimo valor no array

if(!in_array($ext, $formatos)){
    //Verifica se a extensão NÃO está no array de formatos
    $resultado['ok'] = false;
    $resultado['msg'] = 'O formato do arquivo não é aceito';
    return $resultado;
}

if($file['size'] > $tamanhoMaximo){
    $resultado['ok'] = false;
    $resultado['msg'] = 'Seu arquivo excede o tamanho máximo (10MB)';
}


$arquivo = uniqid('file_') . $ext;
//uniqid retorna o prefixo especificado (file_) seguido por uma ID de 13 caracteres baseado no milionésimo de segundo atual.

$check = move_uploaded_file($file['tmp_name'], $dir.$arquivo);
if(!check){
    //Verifica se a operação para mover o arquivo NÃO foi bem sucedida
    $resultado['ok'] = false;
    $resultado['msg'] = 'Houve um erro ao fazer o upload';
}

return $resultado;

}


if ((isset($_POST["form"])) && ($_POST['form'] == "form1")) {
    $upload = uploadFile($_FILES['arquivo']);
    if(!$upload['ok']{
        //Se $upload['ok'] NÃO for true
        echo 'ERRO: ' . $upload['msg'];
    }
}

?>

Browser other questions tagged

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