The intent of this is to have users submit applications to the client and for the client to review their information and select the best application. I am trying to display a document from a file server onto the php web page. MySQL code holds the path of the file and I'm using readfile(). I thought maybe it was the encoding or maybe it was the file type but nothing I do seems to be working. I tried using a different filetype and I tried using mb_convert_encoding to modify the encoding.
<?php foreach ($applications as $application) : ?> <p> Name: <?php echo $application['fname']; ?> <?php echo $application['mname']; ?> <?php echo $application['lname']; ?><br /></p>
<?php
readfile ($file); ?>
<?php endforeach; ?>
Here's an example of the symbols that are displaying:
PK!���Ʌ)[Content_Types].xml �(�̕MK�#���!�U�m+�H��8j� ^��I���N�������Vۊ�#23���0;� �F'sQ9��N�f X�re'{=��YQ�\hg!c+�l�??�VbB�6fl��o8�r F��y�)\0�5L��ML�w��+.�E���R��{wP����~I�k�:��N,�2&��J �8����Kk�Re����J|�C��]�D� *�d(> CY|�B�s'g�*��28]Q( M}�惓#��贉���&9��̫�!�ap>v�iDK=������p���LD\i�ǟ�Zw�= R�)��;0~>�'� ���X��1�H�������fI����v �8���,�[~��oI���A��s��]�#�� �������PK!�U~��_rels/.rels �(���MK1���!̽;�"��^D�Md�C2��������(�.Ե�3y��3C֛��+�4xW��(A������yX܂JB���Wp����b��#InJ�����E�b�=[J���M�%���a �B�,o0�f#=a��� n�����o�A��;�N�
The file I'm trying to read is .doc file.
$file seems undefined; assuming $application holds all the application data you want to display to the client, then you should be doing:
<?php readfile($application['file']); ?>
If this isn't working then you aren't using the correct path - try debugging like this:
<?php var_dump($application['file'], file_exists($application['file'])); ?>
Related
I need to generate PDF files from HTML templates and plan on using wkhtmltopdf to do that. Inside the HTML templates, I need to be able to use PHP logic to adjust what the template will render. Take this HTML template for example:
<p>Dear <?php echo $firstname; ?>,</p>
<p>Thanks for signing up. You've invited these people along with you:</p>
<ul>
<?php foreach ($invitees as $invitee): ?>
<li><?php echo $invitee; ?></li>
<?php endforeach; ?>
</ul>
<p>Regards,</p>
<p>Chris White</p>
I have no problem being able to pass a HTML template file to wkhtmltopdf but I don't know how to get the PHP logic inside it to run correctly and be able to return the resulting template. I came across this blog post while Googling but the author uses Smarty as a template language: https://davejamesmiller.com/blog/php-html-pdf-conversion-using-wkhtmltopdf
Using Smarty would solve my problem but I don't want to bring in a library to do this when I can just use plain old PHP. Basically, I need a way to pass in variables to the HTML template (in this case $firstname and $invitees), have it execute the PHP code inside the template and then return the resulting template after the PHP has been executed.
Any ideas?
Just save your file as php (for example template.php) and implement there the logic you need.
I did the same also with wkhtmltopdf and it worked great.
I also passed some variables over GET to the template to really get the correct report as pdf.
To save the file I used the PHP session id and saved the file to a folder with write permissions for www-data (Linux) and started the download automatically via Javascript.
I had the same need and I did it like that. Don t really know if my code will help you but why not.
First I used composer to get https://github.com/mikehaertl/phpwkhtmltopdf.
lets say you have a php file " content.php "
<?php
echo "<html>";
echo "<h1>test</h1>";
echo "<html>";
?>
your index.php will be :
<?php
require "vendor/autoload.php";
ob_start();
require('content.php'); //The php file
$content = ob_get_clean();
$pdf = new \mikehaertl\wkhtmlto\Pdf($content);
if (!$pdf->send()) {
throw new Exception('OMG WHY : '.$pdf->getError());
}
If you're not using any template engine, can't u just call your template.php file with some params ?
Something like
$wkhtml2pdf->html2pdf('template.php?firstname=Foo');
(I have no idea how wkhtml2pdf works, this code is just for you to understand the logic)
and in your template.php file :
<p>Dear <?php echo $_GET['firstname']; ?>,</p>
I have created an index.php page in MAMP.
My index.php reads exactly like the following. I access it through localhost:8888.
<?php
echo file_get_contents("http://stackoverflow.com");
?>
However, instead of returning the html source code from this page as I believe it would do, it just returns http://stackoverflow.com as a regular webpage, like the webpage you are looking at now.
My MAMP is using PHP 5.5.10. The user_agent is set and allow_url_fopen is on.
I am severely confused. I would very much appreciate any explanations :)
It IS returning the html and the browser is interpreting it.
You can try wrap the output in tags:
<?php
echo '<code>' . file_get_contents("http://stackoverflow.com") . '</code>';
?>
Or set headers as text/plain instead of html:
<?php
header('Content-Type: text/plain');
echo file_get_contents("http://stackoverflow.com");
?>
Or if you want to keep the headers and not inject the output into code tags:
<?php
echo htmlspecialchars(file_get_contents("http://stackoverflow.com"));
?>
I prefer the last one.
If you want to see the plain text you can use the following,
<?php
header('Content-Type:text/plain');
echo file_get_contents("http://stackoverflow.com");
?>
What you see in your version is correct, since the HTML is rendered by your internet browser.
The results of a php script are by default sent to the bowser, so your code
<?php
echo file_get_contents("http://stackoverflow.com");
?>
Is reading the web page and then sending it to your browser. So it looks like it is just showing the page you read.
If you change it to
<?php
$page = file_get_contents("http://stackoverflow.com");
?>
Then you can do something with the web page source stored in $page.
i am trying to use a php script in my html website. The php script is working as intended when I run it. My php script:
<?php
mysql_connect("localhost","demo","123abc");
mysql_select_db("demo");
$sql=mysql_query("SELECT vorname FROM users");
if(mysql_num_rows($sql)){
$select= '<select name="select">';
while($rs=mysql_fetch_array($sql)){
$select.='<option value='.$rs['vorname'].'>'.$rs['vorname'].'</option>';
}
}
$select.='</select>';
echo $select;
?>
Now what I want is, to have the dropdown on my website on different locations. My idea was to just insert the php script whenever I need it. My html part:
<td>
<?php include 'http://localhost/Testsamples/test.php'; ?>
</td>
But it is not working and I don't know why.
Can someone help me out?
Thanks
You don't include the file by its URL, you must use the system filepath:
<td>
<?php include '/path/to/Testsamples/test.php'; ?>
</td>
You can't include using URL (like http://www.domain.com/script.php).
Use relative path like :
<?php include "/path/test.php"; ?>
Use require is better than include, you will have a direct error if file is not found.
In order to include it from an absolute path you need to have allow_url_include enabled on your php.ini.
See here: http://phpsec.org/projects/phpsecinfo/tests/allow_url_include.html
However, if your file is on the same system, you should use relative urls. i.e. include 'testSamples/test.php (or something similar). You could also use basic folder nav ( e.g. ../testSamples/test.php or use an include relative to root: /public_html/foo/bar/testSamples/test.php)
Try this
<?php /* At the first instance of call, to eliminate multiple opening of SQL connections for the smae data */
ob_start();
get_file_contents('http://localhost/Testsamples/test.php');
$contents = ob_get_contents();
?>
<td>
<?php echo $contents; ?>
</td>
edit - as pointed out in the other answers, you probably do not want to be including absolute paths!
Is it possible to send PHP code to the browser?
The site currently have PHP and jQuery available.
For example:
<?php echo "HELLO"; ?>
this will show as HELLO on the client side:
Hello
I want to make it so the client/browser will receive:
<?php echo "HELLO"; ?>
Possible structure:
index.php (Main Page)
edit.php (A page that will show the source code of index.php including the PHP ad allows me to edit it (and will save server-side))
This is for a temporal on-the-site source code editing (because I cannot access cPanel in certain places).
edit.php
<?php
$cont = file_get_contents('index.php');
echo $cont;
?>
Not like such. Whenever the interpreter sees
<?php echo "HELLO"; ?>
In order to get the contents from an external file you could use file_get_contents('filename.php');
or
<?php echo htmlentities(file_get_contents('filename.php')); ?>
PHP has a built in syntax highlighter which could help you when outputting the PHP code.
$string = '<?php echo "abc..."; ?>';
highlight_string($string);
Alternatively you could output a PHP file with syntax highlighting by using
highlight_file('index.php');
If I have a piece of code that reads a chunk of HTML from a txt file and then echos that html onto the page, how can I accomplish the same task, but when there is PHP inside of the txt file?
ex:
this is the file being read:
<?php
$filecontent = // read some other file
echo($filecontent);
?>
and this is the page that is reading the file:
<?php
$code1 = //reading the above file
?>
<html>
<?php echo($code1); ?>
</html>
When you want to process files containing PHP code you need to use include instead of echo.
<?php include('your_php_file_name'); ?>
If you have the contents of the file in a string you are in a tough spot because the only way to process the code is eval, and in addition you have to properly set up any environment that the code requires. eval itself should be avoided, and the latter is impossible to do in the general case.
Use include instead of echo:
<?php include($file_that_contains_php); ?>
you need to include the first file and echo statement in the first file will get executed.
<html>
<?php require_once("firstfile.php"); ?>
You need to echo htmlentities($code1), because when you echo then browser will not show it contents, because it try to parse it as a html tag, but htmlentities will encode to safe html output this characters.
If you want to evaulate the code, then you need eval($code1) or include it.