Penggunaan fungsi FILEBASE64 pada PHP

View Discussion

Show

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The base64_encode() function is an inbuilt function in PHP which is used to convert any data to base64 encoding. In order to convert an image into base64 encoding firstly need to get the contents of file. This can be done with the help of file_get_contents() function of PHP. Then pass this raw data to base64_encode() function to encode.

    Required Function:

    • base64_encode() Function The base64_encode() function is an inbuilt function in PHP which is used to Encodes data with MIME base64. MIME (Multipurpose Internet Mail Extensions) base64 is used to encode the string in base64. The base64_encoded data takes 33% more space then original data.
    • file_get_contents() Function The file_get_contents() function in PHP is an inbuilt function which is used to read a file into a string. The function uses memory mapping techniques which are supported by the server and thus enhances the performances making it a preferred way of reading contents of a file.

    Input Image:

    Penggunaan fungsi FILEBASE64 pada PHP

    Program:

    Output:

    iVBORw0KGgoAAAANSUhEUgAAApsAAAC4CAYAAACsNSfVAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJ
    cEhZcwAADsQAAA7EAZUrDhsAAAAZdEVYdFNvZnR3YXJdhfdsglgklAEFkb2JlIEltYWdlUmVhZHlxyWqwrwtwefd
    ...
    TeUsalQKBQKhUKhsBvK2FQoFAqFQqFQ2A1lbCoUCoVCoVAo7IYyNhUKhUKhUCgUdkMZmwqFQKBQKO0H0fxpZ1bfc

    Reference:

    • http://php.net/manual/en/function.base64-encode.php
    • http://php.net/manual/en/function.file-get-contents.php

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

    A new platform for documentation and tutorials is launching soon.

    We are migrating Terraform documentation into HashiCorp Developer, our new developer experience.

    Search Terraform documentation

    »filebase64 Function

    filebase64 reads the contents of a file at the given path and returns them as a base64-encoded string.

    The result is a Base64 representation of the raw bytes in the given file. Strings in the Terraform language are sequences of Unicode characters, so Base64 is the standard way to represent raw binary data that cannot be interpreted as Unicode characters. Resource types that operate on binary data will accept this data encoded in Base64, thus avoiding the need to decode the result of this function.

    Terraform uses the "standard" Base64 alphabet as defined in RFC 4648 section 4.

    This function can be used only with functions that already exist as static files on disk at the beginning of a Terraform run. Language functions do not participate in the dependency graph, so this function cannot be used with files that are generated dynamically during a Terraform operation.

    »Examples

    > filebase64("${path.module}/hello.txt")
    SGVsbG8gV29ybGQ=
    

    • file also reads the contents of a given file, but interprets the data as UTF-8 text and returns the result directly as a string, without any further encoding.
    • base64decode can decode a Base64 string representing bytes in UTF-8, but in practice base64decode(filebase64(...)) is equivalent to the shorter expression file(...).

    Sebenarnya Windows memang memiliki utilitas yang mengkodekan dan mendekode base64 - CERTUTIL

    Saya tidak yakin versi Windows apa yang memperkenalkan perintah ini.

    Untuk menyandikan file:

    certutil -encode inputFileName encodedOutputFileName
    

    Untuk memecahkan kode file:

    certutil -decode encodedInputFileName decodedOutputFileName
    

    Ada sejumlah kata kerja dan opsi yang tersedia untuk CERTUTIL.

    Untuk mendapatkan daftar hampir semua kata kerja yang tersedia:

    certutil -?
    

    Untuk mendapatkan bantuan pada kata kerja tertentu (-encode misalnya):

    certutil -encode -?
    

    Untuk mendapatkan bantuan lengkap untuk hampir semua kata kerja:

    certutil -v -?
    

    Secara misterius, kata kerja -encodehex tidak terdaftar dengan certutil -? atau certutil -v -?. Tetapi dijelaskan menggunakan certutil -encodehex -?. Ini adalah fungsi praktis lainnya :-)

    Memperbarui

    Mengenai komentar David Morales, ada kata kerja opsi tipe yang tidak didokumentasikan ke -encodehex yang memungkinkan pembuatan string base64 tanpa baris header atau footer.

    certutil [Options] -encodehex inFile outFile [type]
    

    Suatu tipe 1 akan menghasilkan base64 tanpa baris header atau footer.

    Lihat https://www.dostips.com/forum/viewtopic.php?f=3&t=8521#p56536 untuk daftar singkat dari format tipe yang tersedia. Dan untuk melihat lebih dalam format yang tersedia, lihat https://www.dostips.com/forum/viewtopic.php?f=3&t=8521#p57918 .

    Tidak diselidiki, tetapi kata kerja -decodehex juga memiliki argumen tipe trailing opsional.