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
Related
I am trying to create a .xlsm excel file while reading the macrosCode from the vbaProject.bin file.
I implemented the following snippet:
$filename = 'private://zips/xl/vbaProject.bin';
$handle = fopen($filename, "rb");
$contents = stream_get_contents($handle);
fclose($handle);
$spreadsheet->setMacrosCode($contents);
This is a workaround because the PhpSpreadsheet library does not support generating excel file with macros. But somehow, when reading the vbaProject.bin file, the format seems incorrect, might be because of the encoding. https://i.stack.imgur.com/7SGbK.png
My question is: could I read the vbaProject.bin somehow with the proper format?
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
We are trying to read our website as a file in php and the fread function only can read 255 chars , can anyone help us in this problem?
Code:
$filename = "oururl";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
Also when we are trying to read file through file_get_content the same result is appeared to us for only 255 chars.
You're trying to get a size of a remote source/file specified by URL. filesize function is able to get size of file if URL wrapper supports/provides file statisticsIn your case, http or https could be used for URL wrapper.
$filename = "http://www.someurl.com";
But Stateless HTTP/HTTPS protocols don't provide file statistics.
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);