saving blob file in oracle using php - php

please tell me how can i insert pdf or doc files into oracle blob field .
this is the code which i use for varchr data type and this is fine but how can i save the files into blob data type please help me !!!
if(isset($_POST['elm1'])) {
$pdata=$_POST['elm1'];
$profile_name=$_POST['profilename'];
$profile_id=$_POST['profileid'];
$query = "insert into prepaid_profiles values('$profile_id' , '$profile_name','$pdata')";
$result = oci_parse($dbc,$query);
oci_execute($result);
oci_close($dbc);

When user uploads file, save it in server, show only link to it. When saved text from editor, you save only link (with rest of text). File is still on server.
But if you really need to make your database huge and slowly, read uploaded file with file_get_contents, that convert it to base64 and save to database long string (no idea why it's better).
TinyMCE don't need file content in <textarea>.

Related

Converting HTML output to BLOB image [duplicate]

This question already has answers here:
Convert HTML to image in php
(2 answers)
Closed 3 years ago.
I want to make a some step of saving data into DB as BLOB data type. I need convert data to BLOB in the DB. I have the data that are considered as HTML output in the some file. The selected programming language is PHP with Zend Framework 1 and database is MySQL.
I tried to save HTML page as valid BLOB image in DB, but it is still not working. I can't find any solutions about it and I don't know how to do. When I got a content from URL and saved it into DB as BLOB by using function file_get_contents($url), that is worked to me fine.
Here is a code that saves the content of the HTML page in the file described below into DB as BLOB.
$file = "C:\test.html";
$fp = fopen($file, 'r');
$content = fread($fp, filesize($file));
fclose($fp);
$dbModel = new MyTable($this->db);
$dbRowSet = $dbModel->find(1);
$dbRow = $dbRowSet->current();
$dbRow->map = $content;
$dbRow->save();
Here is a preview of saved HTML page in DB, but there I can't show any BLOB image.
There is shown BLOB image. I had the URL page and tried to save it into DB.
I expected that the result of saving data (HTML page) into DB should be that DB contains valid BLOB images.
Thank you for your help.
You can download wkhtmltoimage from this link. There is a version for all operating systems so that shouldn't be a problem. Then you can use it like so:
$path="wkhtmltoimg/wkhtmltoimage.exe"; //path to your executable
$url="http://google.com";
$output_path="test.png";
shell_exec("$path $url $output_path");
One thing you want to note is that if PHP is in safe mode, shell_exec will not work and you won't be able to do your conversion.
Credit: #Tom
Convert HTML to image in php
After this you just need file_get_contents() and save blob in the database.

read pdf content from BLOB in MySQL

i have developed a system which parses and stores emails into a MySQL database.
I save the attachments as an array of objects in JSON into a MEDIUMBLOB field.
Everything works almost as expected. At least when saving.
When i try to read the attachments i only get back the images, but not the pdf file.
The BLOB has the following 'dummy' content:
[
{
"filename":"img1.png", "ext":"png", "type": "image/png", "size":4096,
"dispo":"inline", "cid":"123#abc", "content":"base64_encoded binary-
data"
},
{
"filename":"pdf1.pdf", "ext":"pdf", "type": "application/pdf",
"size":4096, "dispo":"attachment", "cid":null,
"content":"base64_encoded binary-data"
}
]
The attachments are written as follows:
json_encode($attachmentsDB,JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT).
I debugged, but somehow when i fetch data BLOB from the DB - in this case - the last element = the pdf-file is not returned. Only the preceding image files.
When i hardcode the data and try my code it works perfectly.
The data isn't truncated either.
It seems that it's not a memory problem
If anybody could give me a hint please... ?
Many thanks in advance
Storing big data in database is bad idea because most case it will make your server slow..
rather you can copy your pdf in a folder by php script then store it's references in sql database...
Now you can easily read your pdf content from database..
Or you can check this
PHP: Upload a Pdf to SQL Database
As mentioned by #Shadow:
Mediumblob is up to 16MB only. Base64 drastically increases the size of the data, so there may be some data truncation even if the original binary size is below 16MB. Pls include the code that stores and retrieves the files from the database.
i modified the field to LONGBLOB and now it seems to work fine! :-)

Saving file name as `BLOB` in database

While I'm sending file ( type="file") to database (PHPmyAdmin), instead of saving with file name it saves as BLOB as shown in the following figure.
I found the the similar question Saving Files as blob in database ajax php pdo
here but didn't help me. My php code is as follows for sending file to the database.
$info = pathinfo($_FILES['file']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = $get.".".$ext; //$newname='newfilename.'.$ext
$target ="folder/".$newname;
$doc= move_uploaded_file( $_FILES['file']['tmp_name'], $target);
$sql="INSERT INTO apply(mobile,doc,position)
VALUES (' $mobile','$newname',' $position')";
$query=mysqli_query($conn, $sql);
The file is sending to target folder (named folder) with the correct required name but Why I'm not getting the same name in the database. What's going wrong in my code?
Thanks in advance.
First of all, you should not tore files into the database directly because of critical performance issues.
Suggested way is to store files in file system, and store their path in database as TEXT or Varchar.
Now coming to your question:
While I'm sending file ( type="file") to database (PHPmyAdmin), instead of saving with file name it saves as BLOB as shown in the following figure.
Every file is composed of binaries but in different order and format. Storing it in database is not actually tricky. Database converts them into array of bytes/ stream of bytes and then stores these byte format data into the table.
When a fetch query is fired, it returns the exact byte stream in response. And at application level, these bytes are treated as files by their respective encoding techniques.
It's worth reading this post and subsequent link in the accepted answer.

Upload image to php webservice and store it in MSSQL database

I am trying to upload image to php web service and store it in MS SQL database. My post data is in json format which has base64 encoded image data.
Base 64 encode and insert sample
$base=base64_encode(file_get_contents("C:\sample\images.jpg"));
$CommandText = "INSERT INTO [M_IMAGES] ([IMAGE_DATA]) VALUES(?) " ;
$ImageStream = imagecreatefromstring(base64_decode($this->imageData));
$RowsAffected = (int)$objDBManager->Exe*emphasized text*cuteNonQuery($CommandText,array(array($ImageStream,
SQLSRV_PARAM_IN,
SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY),
SQLSRV_SQLTYPE_VARBINARY('max'))));
Here $this->imageData contanins base64 encoded image data and IMAGE_DATA datatype is image. I am able to upload and retrieve image successfully in normal upload
Normal upload
$ImagePath = $_FILES[$ImageCtrlName]['tmp_name'];
$ImageStream = fopen($ImagePath, "r");
$RowsAffected = (int)$objDBManager->ExecuteNonQuery($CommandText,
array(array(&$ImageStream,
SQLSRV_PARAM_IN,
SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY),
SQLSRV_SQLTYPE_VARBINARY('max'))));
fclose($ImageStream);
I am getting error sqlsrv_query(): supplied resource is not a valid stream resource
Can anyone point out what is wrong here. Or is there a better way to do it. Help me as I am new to PHP.
The best solution is to save in the DB only the url of the image and save the image in your server.
When you want to print it you only have to search the url in the DB.
Trying to save the image in a DB is a wrong solution.
From what I can understand from your question, your problem stems from trying to store the image on to MSSQL as it's binary form, which is not allowed. What I can do, is advice you to store the image in it's base64 form, which is a string of alphanumeric characters. There is no need to try and store the image as an image as the base64 encoded image, is usually serve-able directly to the client.
TL;DR Just store the image as base64.

How to save and display image into database without using move uploaded or copy functions in php

How to save and display image into database without using move uploaded or copy functions in php ?
Is it possible to do that without saving the images into directories and saving them to database with some datatype for that particular field ?
use blob datatype it is direct save image in the database.
here one example of PHP MySQL BLOB.hope it is help you.
Maybe you could try to get the base64 encoded image and save it to your DB.
$image_as_string = base64_encode( file_get_contents( 'image.jpg' ) )
// now save dat string to your database.
Hope it helps.

Categories