How to get all image properties in upload?

3

I'm here browsing Google but I did not find it clear how to get all image properties at the time of uploading in the script.

I'm using it to get the image:

$imagem = $_FILES['arquivo']['name'];
How to get all properties of the image at the time of upload with $_FILES ?

Question:     

asked by anonymous 07.11.2015 / 18:24

1 answer

3

If you do this:

print_r($_FILES['arquivo']);

It will give you all properties:

Array (
    [name] => arquivo.png
    [type] => image/png
    [tmp_name] => /tmp/php5Wx0aJ
    [error] => 0
    [size] => 15726
)
  • name returns the file name
  • type return mimetype sent by multipartbody (this does not is a reliable method of checking the file type, read below that it will have an alterantiva)
  • tmp_name is the location where the file was uploaded, ie the move_upload_file function only moves the file from this folder.
  • erro return some number other than zero if there is a failure in the upload, otherwise it will 0 which means that everything went well.
  • size is the size of the file.

Note that pro upload works by requiring the enctype="multipart/form-data" attribute, like this:

<form action="getfile.php" method="post" enctype="multipart/form-data">

As stated in this question:

Handling Upload Errors

Note that if the upload returns an error it will be a number through the key [error] , it follows the list of constants that you will use to know which error occurred:

  • UPLOAD_ERR_OK

    Value: 0; there was no error, the upload was successful.

  • UPLOAD_ERR_INI_SIZE

    Value 1; The uploaded file exceeds the limit set in the upload_max_filesize directive of php.ini .

  • UPLOAD_ERR_FORM_SIZE

    Value: 2; The file exceeds the limit defined in MAX_FILE_SIZE in the HTML form.

  • UPLOAD_ERR_PARTIAL

    Value: 3; The file was partially uploaded.

  • UPLOAD_ERR_NO_FILE

    Value: 4; No files were uploaded.

  • UPLOAD_ERR_NO_TMP_DIR

    Value: 6; Temporary folder is missing. Introduced in PHP5.0.3.

  • UPLOAD_ERR_CANT_WRITE

    Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.

  • UPLOAD_ERR_EXTENSION

    Value: 8; An extension of PHP has stopped uploading the file. PHP does not provide a way to determine which extension caused the interrupt. Browsing the list of extensions loaded with phpinfo() can help. Introduced in PHP 5.2.0.

Example usage:

switch ($_FILES['upfile']['error']) {
    case UPLOAD_ERR_OK:
        //Upload funcionou
    break;
    case UPLOAD_ERR_NO_FILE:
        throw new RuntimeException('Arquivo não foi enviado');
    break;
    case UPLOAD_ERR_INI_SIZE:
    case UPLOAD_ERR_FORM_SIZE:
        throw new RuntimeException('Tamanho excedido.');
    default:
        throw new RuntimeException('Erro desconhecido');
}

Checking mimetype

Note that using the [type] key is not an effective method of checking the file type because it does not check the actual data of the upado file.

Checking the extension is also not an effective method, because you can put the extension you want in the file, it is best to check with the finfo function, as I already mentioned in this answer:

07.11.2015 / 18:55