# PHP:save file on mysql

**URL:** <https://forum.freecodecamp.org/t/php-save-file-on-mysql/424601>\
**Category:** General\
**Created:** [October 8, 2020, 12:10am UTC](https://forum.freecodecamp.org/t/php-save-file-on-mysql/424601 "2020-10-08T00:10:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Advitya-sharma](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/advitya-sharma/32/151816_2.png) [@Advitya-sharma](https://forum.freecodecamp.org/u/Advitya-sharma)\
**Post date:** [October 8, 2020, 12:10am UTC](https://forum.freecodecamp.org/t/php-save-file-on-mysql/424601/1 "2020-10-08T00:10:44Z")

</div>

not able to find a article to save the complete file on db , not just the file path.  
please share if you find something

---

<div class="post-metadata">

**Author:** ![skaparate](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/skaparate/32/125896_2.png) [@skaparate](https://forum.freecodecamp.org/u/skaparate)\
**Post date:** [October 8, 2020, 12:59am UTC](https://forum.freecodecamp.org/t/php-save-file-on-mysql/424601/2 "2020-10-08T00:59:32Z")

</div>

Hello!

Are you trying to save any kind of files? [file\_get\_contents](https://www.php.net/manual/en/function.file-get-contents.php) should serve your purpose.

1. Read the file contents with `file_get_contents`.
2. Save the content to the database:

```auto
$the_file_contents = file_get_contents('path/to/file');
$query = "insert into (the_column_name) values ('$the_file_contents')";
// Execute the query
``
```

---

<div class="post-metadata">

**Author:** ![skaparate](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/skaparate/32/125896_2.png) [@skaparate](https://forum.freecodecamp.org/u/skaparate)\
**Post date:** [October 8, 2020, 1:04am UTC](https://forum.freecodecamp.org/t/php-save-file-on-mysql/424601/3 "2020-10-08T01:04:28Z")

</div>

Now that I think of it, you should encode the file contents using something like base64:

```auto
$the_file_contents = base64_encode(file_get_contents('path/to/file'));
$query = "insert into (the_column_name) values ('$the_file_contents')";
// Execute the query

// When reading the values:
$file_contents = base64_decode($encoded_file_contents);
// $encoded_file_contents is the data from the database

```

---

<div class="post-metadata">

**Author:** ![BrillMindz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/brillmindz/32/182458_2.png) [@BrillMindz](https://forum.freecodecamp.org/u/BrillMindz)\
**Post date:** [October 9, 2020, 2:33am UTC](https://forum.freecodecamp.org/t/php-save-file-on-mysql/424601/4 "2020-10-09T02:33:42Z")

</div>

$the\_file\_contents = file\_get\_contents(‘path/to/file’);  
$query = “insert into (the\_column\_name) values (’$the\_file\_contents’)”;  
this should solve the problem

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:38pm UTC](https://forum.freecodecamp.org/t/php-save-file-on-mysql/424601/5 "2021-06-01T18:38:15Z")

</div>

Closed on June 1, 2021, 6:38pm UTC


