move_uploaded_file returns only FALSE

Asked

Viewed 296 times

0

I have a problem I couldn’t identify, move_uploaded_file returns only FALSE regardless of what I pass in the parameters. Follow 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 in $_FILES['img'] returns this array

Array
(
    [name] => RgtoGIN.png
    [type] => image/png
    [tmp_name] => /tmp/phpfLzDGR
    [error] => 0
    [size] => 147270
)
  • Anorak, the solutions should be in the answers, not the question.

1 answer

1


Have you checked what’s worth error the variable $_FILES['img'] ?

The value 'error' returns 0 when there is no error in the upload. PHP provides the constant called UPLOAD_ERR_OK 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 following constants:

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 be done when an upload fails.

In the Documentation of PHP talks about it.

  • Had already done the test to see what returned me, and the value returned in var_dump($_FILES['img']['error']) is 0, returning that move_uploaded_file should work normally correct? But it returns FALSE.

  • What is returned in $_FILES['img']['tmp_name']?

  • Giving a print_r in $_FILES['img'] returns this array: Array ( [name] => Rgtogin.png [type] => image/png [tmp_name] => /tmp/phpfLzDGR [error] => 0 [size] => 147270 )

  • I’m beginning to suspect you’re drinking clay in the way of the archive. Try to do what I will do, no upload, to see if return any error: file_put_contents(BASE_URL."media/xxx.txt", 'hello'). If the file appears in the folder, fine. If not, let me know.

  • I was able to find a very useful answer on the internet, which solved my problem, the big problem is that when passing the path to where the file will be directed there cannot be (http://...) OR (https://...), the correct is to pass only in the Unix style (./directory/file). Doing so the file is passed correctly and saved smoothly. Thanks for the help.

Browser other questions tagged

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