Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Allow store resource into postgresql bytea #32319

Merged
merged 1 commit into from
Apr 10, 2020

Conversation

majsterkoo
Copy link

Allowing store files into postgresql database with bytea column type. You can simple use:

\DB::table('table')->insert([ 'bytea_column' => fopen('filename', 'r') ]);

You can simple storing files from user upload etc...

@GrahamCampbell GrahamCampbell changed the title [7.x] allow store resource into postgresql bytea [7.x] Allow store resource into postgresql bytea Apr 9, 2020
@driesvints
Copy link
Member

We'll need much more info on why this is needed.

@majsterkoo
Copy link
Author

In Postgresql there is bytea column where you can store variable-length binary string (more information: postgres datatype binary. So you can have input with type file on page, where user uploading files. There are no needs to convert uploaded file to base64 or others formats, because you can store it in native postgresql way as bytea. But when you try store files like pdf etc, it fails, becase PARAM_STR not supporting special chars (I tried this, but because it is not working I went deep into problem and found this solution).

You can argue that files can be stored on a server outside the database, but trust me, this is not solution for me. Using PARAM_LOB is standard way (see the php documentation). This is not hacking or tricky way.

I think this cannot be used in Connection.php, because for example mssql needs aditional params (which is demonstrate here: mssql examples).

//ms sql way
$tsql = "INSERT INTO Production.ProductPhoto (LargePhoto)   
 VALUES (?)";  
$uploadPic = $conn->prepare($tsql);  
$fileStream = fopen($_FILES['file']['tmp_name'], "r");  
$uploadPic->bindParam(1,    
  $fileStream,   
  PDO::PARAM_LOB,   
  0,   
  PDO::SQLSRV_ENCODING_BINARY);  
$uploadPic->execute();  

This means that if this patch is not acceptable, I think I can create patch for all databases, but for me, it may be hard to test it (I haven't mssql database). So, please, if you not accept it, tell me, what I can do for acceptable patch.

Which solved this patch for postgresql:

//before patch you can try this, but it should fail (for example txt file can work because there are no mostly special chars).
\DB::table('table')->insert([ 'bytea_column' => stream_get_contents('filename') ]);

Because PARAM_LOB is for resources, I created a condition with is_resource in this commit, so you can simple do which I wrote in first comment and it work like a charm, without any problems.

@taylorotwell taylorotwell merged commit 429e7d6 into laravel:7.x Apr 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants