move_uploaded_file returns only FALSE

0

I have a problem that I could not identify, move_uploaded_file returns only FALSE regardless of what I pass in the parameters. Here is the code:

<form method="POST" enctype="multipart/form-data">
  <input type="file" name="img" />
  <input type="submit" value="Enviar" />
  <input type="hidden" name="MAX_FILE_SIZE" value="1024000" /> 
</form>
$way = BASE_URL."media/";
$name = basename($_FILES['img']['name']);
$uploadfile = $way.$name;

if (move_uploaded_file($_FILES['img']['tmp_name'], $uploadfile)) {
    echo "Estou aqui!";
} else {
    echo "Não foi dessa vez.";
}

Giving a print_r to $_FILES['img'] returns this array

Array
(
    [name] => RgtoGIN.png
    [type] => image/png
    [tmp_name] => /tmp/phpfLzDGR
    [error] => 0
    [size] => 147270
)
    
asked by anonymous 26.10.2018 / 18:30

1 answer

1

Have you checked what has error value $_FILES['img'] ?

The value 'error' returns 0 when there is no upload error. PHP makes the constant called UPLOAD_ERR_OK available for you to check if the upload occurred without error.

Example:

 if ($_FILES['img']['error'] !== UPLOAD_ERR_OK) { 
     exit('Ocorreu um erro ao tentar fazer o upload');
 }

If any other value is returned, you can check through the constants below:

UPLOAD_ERR_INI_SIZE
UPLOAD_ERR_FORM_SIZE
UPLOAD_ERR_PARTIAL
UPLOAD_ERR_NO_FILE
UPLOAD_ERR_NO_TMP_DIR
UPLOAD_ERR_CANT_WRITE
UPLOAD_ERR_EXTENSION

This is the first thing to do when an upload fails.

In the PHP documentation talks about this.

    
26.10.2018 / 18:41