create account

Loading a file into the STEEM blockchain by dehenne

View this thread on: hive.blogpeakd.comecency.com
· @dehenne · (edited)
$45.96
Loading a file into the STEEM blockchain
A few weeks ago, the question came up:

> Is it possible to get files into the STEEM blockchain?

As I have discovered, with a few little tricks you can get everything into the STEEM blockchain.

*The following scripts are for testing purposes and not for productive use*

*I wrote this tutorial for @pcsg-dev, but I don't want to post it with this account for a variety of reasons. I also posted an issue for Steemit and waited some time until it could be reacted to, but it is not taken seriously.*


![bandbreite-computer-daten-1148820.jpg](https://cdn.steemitimages.com/DQmUmN4t6FbSNrQ7B2NxjvScgigK3nP9Komvx4Hsu7iCNfa/bandbreite-computer-daten-1148820.jpg)


### What is needed

- A STEEM account
- One file
- Some know-how in programming
    - Our example scripts are written in PHP

I tried to make the tutorial as simple as possible. 
However, some prerequisites are necessary, so I classify this for experienced users.


### What will you learn:
    
- How to split files with PHP
- How can you upload files to the STEEM blockchain
- How can you reassemble split files with PHP

### Difficulty
- Intermediate


General procedure
------

- We split our file into small pieces
    - This is because STEEM has a maximum length of a post
- We convert each section into text
    - This is because we get binary data only badly in a post
- We write a post / comment for each section 
    - The content of a post is the content of a file
- We read every single post and put all content back together again
    

Tutorial
------

- All examples are written in PHP, but the procedure can be implemented in any language imaginable. 
- Let us take Steemit's white paper as an example.


### Split the file

Splitting the file is easy on the right. 

**Procedure**

- We're looking at the size of the file,
- split the file into many small sections with `fread($file_handle, $buffer)`,
- done

**Partial section Example**


```php
// ...

// how many parts would be existed
$parts = $file_size / $buffer;

// ...

for ($i = 0; $i < $parts; $i++) {
    // read buffer sized amount of the file
    $file_part = fread($file_handle, $buffer);

    // the filename of the part
    $file_part_path = $store_path.$file_name.".part$i";

    // create and write the part
    $file_new = fopen($file_part_path, 'w+');
    fwrite($file_new, $file_part);
    fclose($file_new);
}

// ...

```

You can find the complete example in [split.php](https://github.com/pcsg/steem-tutorials/blob/master/upload-a-file/split.php)


### Upload the file

Here's the interesting part. 
If we now have our small pieces, we have to convert every single piece into text that we can upload as mail.

**Procedure**

- We'll take a section,
- read the contents of the file with `file_get_contents()`,
- convert the binary content to text. For example with `bin2hex()`,
- write a new post with this content

**Example**

```php
// ...

$fileData = bin2hex(file_get_contents('PATH_TO_FILE/steem-whitepaper.pdf.part0'));

// ...
```

You can find the complete example in [upload.php](https://github.com/pcsg/steem-tutorials/blob/master/upload-a-file/upload.php)


### Download the file

For the whole thing to make sense, we have to get the file out of the blockchain.

With the LightRPC Client (https://github.com/hernandev/light-rpc) it is an easy to read single posts via PHP.
If you don't know how to install it, just have a look at https://github.com/hernandev/light-rpc There is a wonderful explanation. 

*If requested, I can also write a small tutorial for you here.*


**Procedure**

- We're reading our summary file,
- get the contents of every post,
- convert this content back to binary data,
- pack all contents into one file.

*In our example file we have moved the download part into a function, so that the whole thing is a bit clearer.*


```php

/**
 * This function fetch a steemit post
 *
 * @param string $username
 * @param string $permlink
 * @return mixed
 */
function getContent($username, $permlink)
{
    $Client = new Client('https://api.steemit.com');

    $Request  = new Request('content_api', 'get_content', [$username, $permlink]);
    $Response = $Client->send($Request);

    if ($Response->isError()) {
        var_dump($Response->error());
        exit;
    }

    $response = $Response->toArray();
    $result   = $response['result'];

    return $result['body'];
}
```


Now, to the "how we get it all together" part.

In our `$summary` we have all our files, we go through them one by one and put our whitepaper 
back into one file.


```php

// now we go through all the files and get the content from the steem blockchain
foreach ($summary as $part => $permlink) {
    echo $part.PHP_EOL;

    // get the content from the steem blockchain with our helper function
    $content = getContent('pscg.test', $permlink);

    // the content is in hex text, but we need binary.
    // if you still remember we converted it for the steem blockchain
    // now we convert it back to binary data
    $content = hex2bin($content);

    // this binary data is now attached to our file
    file_put_contents($filename, $content, \FILE_APPEND);
}
```

You can find the complete example in [download.php](https://github.com/pcsg/steem-tutorials/blob/master/upload-a-file/download.php)


I hope the tutorial was fun and taught you a little bit.

I wish you a lot of fun 
Hen
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
πŸ‘Ž  , , , , , ,
properties (23)
authordehenne
permlinkloading-a-file-into-the-steem-blockchain
categoryutopian-io
json_metadata{"tags":["utopian-io","tutorials","steem"],"users":["pcsg-dev"],"image":["https://cdn.steemitimages.com/DQmUmN4t6FbSNrQ7B2NxjvScgigK3nP9Komvx4Hsu7iCNfa/bandbreite-computer-daten-1148820.jpg"],"links":["https://github.com/pcsg/steem-tutorials/blob/master/upload-a-file/split.php","https://github.com/pcsg/steem-tutorials/blob/master/upload-a-file/upload.php","https://github.com/hernandev/light-rpc","https://github.com/pcsg/steem-tutorials/blob/master/upload-a-file/download.php"],"app":"steemit/0.1","format":"markdown"}
created2018-07-11 04:37:57
last_update2018-07-11 05:03:21
depth0
children6
last_payout2018-07-18 04:37:57
cashout_time1969-12-31 23:59:59
total_payout_value34.872 HBD
curator_payout_value11.085 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,362
author_reputation8,032,043,781,732
root_title"Loading a file into the STEEM blockchain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,240,370
net_rshares22,406,930,778,989
author_curate_reward""
vote details (58)
@balticbadger ·
Really cool! I will give this a try
properties (22)
authorbalticbadger
permlinkre-dehenne-loading-a-file-into-the-steem-blockchain-20180711t051533017z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-07-11 05:15:33
last_update2018-07-11 05:15:33
depth1
children0
last_payout2018-07-18 05:15:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length35
author_reputation68,520,530,759,841
root_title"Loading a file into the STEEM blockchain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,243,392
net_rshares0
@mcfarhat ·
Hey @dehenne, 
That is such a cool idea, and nice approach to implementing it!
As @portugalcoin mentioned, following the template is normally preferred, yet your post does bring additional value of course to our tutorials.
It would have been interesting to see this as a live example with actual screenshots and outcome. If you did not want to try this out on live, maybe you can check on a Steem testnet https://developers.steem.io/testnet/ ? I haven't tried it myself but could be worthy in exploratory situations.

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/21233122).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  ,
properties (23)
authormcfarhat
permlinkre-dehenne-loading-a-file-into-the-steem-blockchain-20180712t082525252z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["dehenne","portugalcoin"],"links":["https://developers.steem.io/testnet/","https://join.utopian.io/guidelines","https://review.utopian.io/result/8/21233122","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-07-12 08:25:51
last_update2018-07-12 08:25:51
depth1
children1
last_payout2018-07-19 08:25:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,008
author_reputation150,651,671,367,256
root_title"Loading a file into the STEEM blockchain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,383,001
net_rshares8,258,387,566
author_curate_reward""
vote details (2)
@dehenne ·
Thank you for the approving. In real live example is to be enjoyed with caution. After my vacation I can think about setting something up for the Testnet. ;-)
properties (22)
authordehenne
permlinkre-mcfarhat-re-dehenne-loading-a-file-into-the-steem-blockchain-20180712t091914944z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-07-12 09:19:15
last_update2018-07-12 09:19:15
depth2
children0
last_payout2018-07-19 09:19:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length158
author_reputation8,032,043,781,732
root_title"Loading a file into the STEEM blockchain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,387,292
net_rshares0
@portugalcoin · (edited)
Thank you for your contribution.

- Doesn't follow the template for the tutorials <a href="https://github.com/utopian-io/editor-templates/blob/master/tutorials">link</a> :  You don't have the <b>repository</b> and <b>Proof of Work Done</b>.

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  
properties (23)
authorportugalcoin
permlinkre-dehenne-loading-a-file-into-the-steem-blockchain-20180711t204411066z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://github.com/utopian-io/editor-templates/blob/master/tutorials","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-07-11 20:44:09
last_update2018-07-12 21:35:27
depth1
children0
last_payout2018-07-18 20:44:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length410
author_reputation598,828,312,571,988
root_title"Loading a file into the STEEM blockchain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,330,964
net_rshares5,254,553,552
author_curate_reward""
vote details (1)
@steemitboard ·
Congratulations @dehenne! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png)](http://steemitboard.com/@dehenne) Award for the number of upvotes

<sub>_Click on the badge to view your Board of Honor._</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



> Support [SteemitBoard's project](https://steemit.com/@steemitboard)! **[Vote for its witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1)** and **get one more award**!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-dehenne-20180917t060801000z
categoryutopian-io
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2018-09-17 06:08:00
last_update2018-09-17 06:08:00
depth1
children0
last_payout2018-09-24 06:08:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length672
author_reputation38,975,615,169,260
root_title"Loading a file into the STEEM blockchain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id71,510,623
net_rshares0
@utopian-io ·
Hey @dehenne
**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
πŸ‘  
properties (23)
authorutopian-io
permlinkre-loading-a-file-into-the-steem-blockchain-20180712t083508z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-07-12 08:35:09
last_update2018-07-12 08:35:09
depth1
children0
last_payout2018-07-19 08:35:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length299
author_reputation152,955,367,999,756
root_title"Loading a file into the STEEM blockchain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,383,671
net_rshares5,637,602,769
author_curate_reward""
vote details (1)