How to get all image properties uploaded?

Asked

Viewed 435 times

3

I am here browsing on Google but I did not find in a clear way how to get all properties of the image at the time of uploading in the script.

I’m using it to get the image:

$imagem = $_FILES['arquivo']['name'];

Question: How to get all image properties when uploading with $_FILES?

1 answer

3


If you do so:

print_r($_FILES['arquivo']);

He’ll give you all the 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 is not is a reliable method of checking the file type, read down will have an alterntiva)
  • tmp_name is the place where the file was uploaded, ie the function move_upload_file just move the file from this folder.
  • erro return some non-zero number if there is any upload failure, otherwise it will be 0 which means it all went well.
  • size is the file size.

Note that pro upload function requires the attribute enctype="multipart/form-data", thus:

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

As stated in this question: Upload does not work $_FILES Undefined index error

Handling upload errors

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

  • UPLOAD_ERR_OK

    Value: 0; no error, upload was successful.

  • UPLOAD_ERR_INI_SIZE

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

  • UPLOAD_ERR_FORM_SIZE

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

  • UPLOAD_ERR_PARTIAL

    Value: 3; The file has been partially uploaded.

  • UPLOAD_ERR_NO_FILE

    Value: 4; No file sent.

  • UPLOAD_ERR_NO_TMP_DIR

    Value: 6; Temporary folder missing. Entered in PHP5.0.3.

  • UPLOAD_ERR_CANT_WRITE

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

  • UPLOAD_ERR_EXTENSION

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

Example of use:

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 out the mimetype

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

Check the extension also not an effective method as you can put the extension you want in the file, it is best to do the check with the function finfo, as I have already quoted in this reply:

  • 1

    Very good this answer and the other complements very well this :)

  • @Marcosvinicius Thanks! I added some more details.

Browser other questions tagged

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