I have a PDF file on my server that I want to select and transform into a blob for insertion into my database (using an INSERT INTO command). My first problem is getting hold of the PDF using PHP. I know it is done with the file_get_contents() function, but I do not understand what parameters it needs.
$fp = fopen($fileLocation, 'r');
$content = fread($fp, filesize($fileLocation));
$content = addslashes($content);
fclose($fp);
You can save the $content to blob field in mysql
Related
I'm trying to make a log system using mongodb in php and GridFS. I can initially write data to my file but once I close the stream I don't know how to append data later to it. This is how I write to it:
$bucket= DB::connection('mongodb')->getMongoDB()->selectGridFSBucket();
$stream = $bucket->openUploadStream('my-file-name.txt');
$contents = 'whatever text here \n';
fwrite($stream, $contents);
fclose($stream);
I tried retreiving it and appending data to the stream but it doesn't work. This is attempt:
$bucket= DB::connection('mongodb')->getMongoDB()->selectGridFSBucket();
$stream = $bucket->openDownloadStreamByName('my-file-name.txt');
fwrite($stream, $contents);
also tried fopen on the stream but no luck. I also don't know how to retrieve this file Id after writing data to it.
I couldn't find a simple solution to append so I ended up replacing the previous file with a new file and deleting the old one.
$stream = $bucket->openDownloadStream($this->file_id);
$prev_content = stream_get_contents($stream);
//delete old data chunks
DB::connection('mongodb')->collection('fs.chunks')->where('files_id',$this->file_id)->delete();
//delete old file
DB::connection('mongodb')->collection('fs.files')->where('_id',$this->file_id)->delete();
$new_content =$prev_content.$dt.$msg."\n"; // append to log body
$new_filename = $filename_prefix;
$stream = $bucket->openUploadStream($new_filename);
fwrite($stream, $new_content);
fclose($stream);
$new_file = DB::connection('mongodb')->collection('fs.files')->where('filename',$new_filename)->first();
UPDATE:
For future readers, after using this system for few months I realized this is not a good solution. Files & chunks can get corrupted and also the process is super slow. I simply switched to logging in a txt file and just storing a reference to my file in mongodb much better :)
I'm using php in order to stream a file as a download. A part of this uses readfile:
<?php
// headers etc...
// ...
readfile('file.exe');
?>
This is a binary file, but for various reasons (that are not relevant to this question) I need to store this file as base64 or similar.
How do I stream a file with readfile that is stored encoded with base64?
I guess there are many ways that lead to success here, but I'm looking for the best & most convenient.
Using stream filters should help:
$inFile = 'file.exe';
$outFile = 'php://output';
$inHandle = fopen($inFile, 'r');
$outHandle = fopen($outFile, 'w');
stream_filter_append($inHandle, 'convert.base64-decode');
stream_copy_to_stream($inHandle , $outHandle);
fclose($inHandle );
fclose($outHandle);
If the file is not too big you can read it using file_get_contents and then decode it with base64_decode:
<?php
$content = file_get_contents('file.exe');
echo base64_decode($content);
I want to retrieve a ZIP file with PHP from a MSSQL database wich is stored in a IMAGE field.
In this part i make a connection using sqlsrv, send a query, and move to the first row and get the first field in BINARY encoding.
$conn = sqlsrv_connect($sql['s'],array('Database'=>$sql['db'],'UID'=>$sql['usr'],'PWD'=>$sql['pwd']));
$q = 'SELECT TOP 1 FileContent
FROM dbo.tblDocumentContent
WHERE FileContent IS NOT NULL
ORDER BY CreateDate DESC';
$res = sqlsrv_query($conn, $q);
sqlsrv_fetch($res);
$zip = sqlsrv_get_field($res,0,SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY));
At this point the zip is retrieved as a resource stream and here i get stuck.
When i set the headers and output the content, the browser downloads the zip.
header("Content-type: application/zip, application/octet-stream");
header("Content-Disposition: attachment; filename=test.zip;");
fpassthru($zip);
This works like a charm, the zip works perfect.
But what i want is to open the zip serverside without the user having to dowload it.
So when i just try to write the content to a file:
$file = fopen('test.zip', 'a');
fwrite($file, fpassthru($zip));
fclose($file);
It can't be opened. I figured that when the browser downloads the given content, it encodes it someway. Alltrough i can not figure out how to do that while writing it to a file.
If someone has any solutions to write the resource stream to a file on the server side in the proper way, that would be great.
This should solve the problem:
$file = fopen('test.zip', 'w');
while (!feof($img)) {
$chunk = fread($img, 1024);
fwrite($file, $chunk);
}
fclose($img);
fclose($file);
I have been tryign to get the raw contents of a PDF file using PHP to POST to an API using CURL. I have tried using file_get_contents (though the resulting file is not readable) and I have tried
$filename = "/files/example.pdf";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
Then I post contents and again the file is not readable by a PDF reader
Any idea's?
Thanks
I have a problem in saving file data and retrieving content in original form using SQL Server.
To save content, I have used this code
$size = filesize($file);
$fp = fopen($file, 'rb');
$content = fread($fp, $size);
$content = addslashes($content);
fclose($fp);
and database to store its content is of type image.
Never do addslashes to binary files.addslashes should only be done on text data.
Image files,audio video files and executable all are binary files.
Try removing addslashes and try again.If it still doesn't get inserted into your database then you should check if the datatype of the column in your database is set to blob.Only blob datatype can hold binary data.
If still you have problems inserting to database then try inserting smaller images of size smaller than 1mb. php has a default post and file upload limit of 2mb.