to fetch doc file content - php

$myFile = "pdf/document_file.doc";
$fh = fopen($myFile, 'r');
$theData = file_get_contents($myFile);
fclose($fh);
echo "<pre>";
echo $theData;
echo "</pre>";
My above script fetches the data from document file but it display some of binary numbers.
I need to display document file content in to html content
For Example
If my document file is a resume. i need to fetch the content and display document data same as selected file in html page

It IS binary data. To view it as HTML you need to convert it first.
Convert .doc to html in php

You're reading binary data. Hence the "numbers". The raw data needs to go through some "filter/render implementation" first.
Plus, when you use file_get_contents() there's no need to fopen() and fclose().

Related

How to send binary data over JSON object which I've got by reading PDF file?

I'm reading a PDF file content using php fread function. Now I want to post this data through JSON object. So, how can I send this data(content of PDF file)?
I'm using POSTMAN to send pdf file by selecting binary in header option and getting desired result. But how can I achieve this programatically using PHP.
$readfile = fopen("newfile.pdf", "r") or die("Unable to open file!");
echo fread($readfile,filesize("newfile.pdf"));
fclose($readfile);
Using this fread method I'm getting content of 'newfile.pdf'.
I want to send content of 'newfile.pdf' as a JSON object without base64 encode or decode.

readfile('filename.exe') and decode base64?

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);

Reading pdf or word file with php

how can i open and read file PDF or Word file with php ??
<?php
echo "hello";
$myfile = fopen("SoftwareTesting&Engineering_day1.pdf", "r") or die("Unable to open file!");
echo fread($myfile,filesize("SoftwareTesting&Engineering_day1.pdf"));
fclose($myfile);
?>
You cannot do it with php fopen(). You may be able to display some junk data only. Because word documents and pdf files are not simple text files rather complicated than plain text. If you have a simple text file then you can go along with php file handling functions. If you really want to manipulate word documents and pdf documents then you have to use a good library for that.
https://github.com/PHPOffice/PHPWord
http://phpword.codeplex.com/
https://github.com/smalot/pdfparser
are some of them.
You could set the header to the content type of the file (see PHP.net for information). Note: You should not print out any contents before setting the header. Also note that the client needs correct plugins to display the file. Otherwise it will be offered to download or you see the binary content of the file.
Then you could echo file_get_content(path) to print out the file content itself.
(untested).

write the data to saved excel without affecting the format

I have saved excel sheet on my server,it has an format i trying to write data to the saved excel but when i trying write its removing the excel format (i.e it removing header color everything) it write start data in the first cell,i want to write the data to saved excel without changeling the excel format start from cell 10, below is the image how my excel looks like before performing the function.
and after how its,looks like.
before:
after
mycode
<?php
$File = "Book1.xls";
$Handle = fopen($File, 'w');
$Data = "Abdul Rahim\n";
fwrite($Handle, $Data);
$Data = "Raja\n";
fwrite($Handle, $Data);
print "Data Written";
fclose($Handle);
?>
can anyone help me how to do this .
You cannot modify the XLS just with fwrite. It has a strict format.
You could switch to CSV or use a third-party library like PhpExcel.

Output Contents of PHP File for a GET/POST request

I have a script which generates an XML file when the user accesses the link like so:
domain.com/dirList.php
I would like to print the contents of the XML to the user at the end of the POST or GET request (have not implemented this yet). At the moment I am just viewing the link through a web browser.
This is the code I am using to print the file contents.
# Open XML file and print contents
$filename = "test.xml";
$handle = fopen ($filename, "r");
$contents = fread ($handle, filesize ($filename));
fclose ($handle);
print $contents;
The problem is that it doesn't seem to be working. I think I might be either missing a header like this: ("Content-Type: text/xml"); or maybe my code is wrong.
What do I need to do to get the XML file to print to the browser?
You can just do:
header('Content-type: text/xml'); // set the correct MIME type before you print.
readfile('test.xml'); // output the complete file.
//Check there is any extra space before <? and after ?>
//or
header('Content-type: text/xml');
echo file_get_contents($path.$xmlfile);

Categories