PHP:save file on mysql

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


Are you trying to save any kind of files? file_get_contents should serve your purpose.

  1. Read the file contents with file_get_contents.
  2. Save the content to the database:
$the_file_contents = file_get_contents('path/to/file');
$query = "insert into (the_column_name) values ('$the_file_contents')";
// Execute the query
1 Like

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

$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
1 Like

$the_file_contents = file_get_contents(‘path/to/file’);
$query = “insert into (the_column_name) values (’$the_file_contents’)”;
this should solve the problem

1 Like