creating a textbox from a non-txt fopen file - php

I have the following code that opens a non-txt file and runs through it so it can read the file line by line, i want to create a textbox (using html probably) so i can put my readed text into that but i have no idea how to do it
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h2>testing</h2>
<?php
$currentFile = "pathtest.RET";
$fp = fopen($currentFile , 'r');
if (!$fp)
{
echo '<p> FILE NOT FOUND </p>';
exit;
}
else
{
echo '<p><strong> Arquivo:</strong> ['. $currentFile. '] </p>';
}
$numLinha = 0;
while (!feof($fp))
{
$linha = fgets($fp,300);
$numLinha = $numLinha + 1;
echo $linha;
}
fclose($fp);
$numLinha = $numLinha -1;
echo '<hr>linhas processadas: ' . $numLinha;
?>
</body>
</html>
i need the textbox area to be in a form so i can define the cols and rows, or there is an way to do it in php ? is there any way to send the readed content to another .php so i can edit the php to an html interface style freely ?

Try echoing the lines between a textarea:
echo "<textarea>";
while (!feof($fp))
{
$linha = fgets($fp,300);
$numLinha = $numLinha + 1;
echo $linha;
};
echo "</textarea>";
You may use \n in order to break lines on the textarea:
echo $linha . "\n";

Related

How to include file in PHP with user defined variable

I am trying to include file in string replace but in output i am getting string not the final output.
analytic.php
<?php echo "<title> Hello world </title>"; ?>
head.php
<?php include "analytic.php"; ?>
index.php
string = " <head> </head>";
$headin = file_get_contents('head.php');
$head = str_replace("<head>", "<head>". $headin, $head);
echo $head;
Output i am getting :
<head><?php include "analytic.php"; ?> </head>
Output i need :
<head><title> Hello world </title> </head>
Note : Please do not recommend using analytic.php directly in index.php because head.php have some important code and it has to be merged analytic.php with head.php and then index.php
To get the desired output :
function getEvaluatedContent($include_files) {
$content = file_get_contents($include_files);
ob_start();
eval("?>$content");
$evaluatedContent = ob_get_contents();
ob_end_clean();
return $evaluatedContent;
}
$headin = getEvaluatedContent('head.php');
string = " <head> </head>";
$head = str_replace("<head>", "<head>". $headin, $head);
echo $head;
Output will be output string not file string :
<head><title> Hello world </title> </head>
I think your approach is pretty basic (you try to hardcore modify - programmerly edit - the template script, right?) but anyway:
$file = file('absolut/path/to/file.php');
foreach ($file as $line => $code) {
if (str_contains($code, '<head>')) {
$file[$line] = str_replace('<head>', '<head>' . $headin, $code);
break;
}
}
file_put_contents('absolut/path/to/file.php', $file);

PHP - Only the path is output instead of the page

I have a small problem with my function, which is supposed to do nothing but output the web page more dynamically via domain.com/index.php?page=start.
My problem is that only the path is displayed, but not the content of the start.html.
My index.php:
<?php
require_once './ext/config.php';
require_once './ext/functions.php';
$page = isset($_GET["page"]) ? $_GET["page"] : "default";
$pc = "$website_pages/$page" .".html";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $website_title; ?></title>
</head>
<body>
<?php echo $pc; ?>
</body>
</html>
My function.php:
<?php
function getPage($pagename) {
global $website_pages;
$path = "$website_pages/$pagename";
if (file_exists($path)) {
return openPage($path);
} else {
echo "Error";
return openPage("$website_pages/includes/default.html");
}
}
function openPage($pageurl) {
$fh = fopen($pageurl, "r");
$fc = fread($fh, filesize($pageurl));
fclose($fh);
return $fc;
}
?>
My config.php:
<?php
$website_title = "Title";
$website_charset = "UTF-8";
$website_pages = "includes";
?>
Output in browser:
includes/start.html
Maybe you can help me?
regards

HTML using a PHP script

im new in php programming and ive a problem recently. I have 1 html page with a Search Box and a php script using for grep in a specific file on local host. This is what i want, when a user type string of char and click on enter that send a POST to modify my php var $contents_list, and grep all filename where the string is found.
<?php
$contents_list = $_POST['search'];
$path = "/my/directory/used/for/grep";
$dir = new RecursiveDirectoryIterator($path);
$compteur = 0;
foreach(new RecursiveIteratorIterator($dir) as $filename => $file) {
$fd = fopen($file,'r');
if($fd) {
while(!feof($fd)) {
$line = fgets($fd);
foreach($contents_list as $content) {
if(strpos($line, $content) != false) {
$compteur+=1;
echo "\n".$compteur. " : " . $filename. " : \n"."\n=========================================================================\n";
}
}
}
}
fclose($fd);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<form action="page2.php" method="post">
<INPUT TYPE = "TEXT" VALUE ="search" name="search">
</form>
</body>
And when i go to my html page and type text in searchbar, that redirect me to my php script "localhost/test.php" and i have 500 internal error.
So I want:
To see result of the php script on the same html page, but i dont know how to do that :/
And if the previous filename return was same like previous result, dont print it, to avoid double result.
I hope its clear and youve understand what i want to do, so thanks for the people who want to help me <3
My recommendations:
Combine the code into the single index.php file for simplicity
Separate logic for search and output to achieve clean separation of duties
Add helper text such as nothing found or enter text
index.php content:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<form action="index.php" method="post">
<input type="text" placeholder="search" name="search">
</form>
<?php
// Check if the form was submitted.
if (isset($_POST['search']) && (strlen($_POST['search'])) > 0) {
$search_line = $_POST['search'];
$path = "/my/directory/used/for/grep";
$dir_content = new RecursiveDirectoryIterator($path);
// Array to store results.
$results = [];
// Iterate through directories and files.
foreach (new RecursiveIteratorIterator($dir_content) as $filename => $file) {
$fd = fopen($file, 'r');
if ($fd) {
while (!feof($fd)) {
$file_line = fgets($fd);
if (strpos($file_line, $search_line) !== FALSE) {
$results[] = $filename;
}
}
fclose($fd);
}
}
// Output result.
echo "<pre>";
if ($results) {
foreach ($results as $index => $result) {
echo ($index + 1) . " :: $result\n";
}
}
else {
echo "Nothing found!";
}
echo "</pre>";
}
else {
// When nothing to search.
echo "<pre>Enter something to search.</pre>";
}
?>
</body>
</html>

How to get the output of php and pass this output to pure html page?

This is a critical scenario.I have 2 pages such as
output.php has dynamic table data with style which is get from excel.
<?php
ob_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'PHPExcel/IOFactory.php';
/* EXCEL TABLE */
$inputFileName2 = 'C:\inetpub\wwwroot\Monthly-EDM\Action_items.xlsx';
try {
$objPHPExcel1 = PHPExcel_IOFactory::load($inputFileName2);
}
catch (Exception $e) {
die('Error loading file "' . pathinfo($inputFileName2, PATHINFO_BASENAME) . '": ' . $e->getMessage());
}
/* Open an Excel & count */
$allDataInSheet = $objPHPExcel1->getActiveSheet()->toArray(null, true, true, true);
$arrayCount = count($allDataInSheet); // Here get total count of row in that Excel sheet
$file = fopen($inputFileName2, "r");
fclose($file);
echo '<table width="800" border="0" cellspacing="0" cellpadding="0" align="center">';
echo '<tr>';
echo '</td>';
/* <th> code here */
echo '</td>';
echo ' </tr>';
echo ' <!-- Header blue row - end -->';
echo '<style>.shiva:nth-child(odd) { background-color:#b9b8bb; }</style>';
echo '<style>.shiva:nth-child(even) { background-color:#e5e8e8; }</style>';
for ($i = 2; $i <= $arrayCount; $i++) {
$_SESSION["a"] = trim($allDataInSheet[$i]["A"]);
$_SESSION["b"] = trim($allDataInSheet[$i]["B"]);
$_SESSION["c"] = trim($allDataInSheet[$i]["C"]);
$_SESSION["d"] = trim($allDataInSheet[$i]["D"]);
$_SESSION["e"] = trim($allDataInSheet[$i]["E"]);
$_SESSION["f"] = trim($allDataInSheet[$i]["F"]);
$_SESSION["g"] = trim($allDataInSheet[$i]["G"]);
echo ' <tr>';
echo '<td>';
echo '</td>';
echo ' </tr>';
}
echo '<!-- table content - end --> ';
echo '</table>';
echo '</td>';
echo ' </tr>';
echo '</table>';
?>
get.html (I want to get the output of output.php here)
Click to view the result of the output.php
How to get the output of output.php and pass this output to get.html page and show the output.Is this possible to access the php output from html page??
Thanks in advance.Please help me to fix it.
Here is one solution (this solution assumes you are using Apache)
Create a .htaccess file at the root of your site. Put this in the .htaccess file:
AddType application/x-httpd-php .html .htm
This file will make Apache parse .html files as PHP. Then in your get.html file, put this code:
<html>
<head></head>
<body><?php include('path/to/output.php'); ?></body>
</html>
If you view the get.html file in your browser, you will see the output of output.php
Here is an alternative solution using Javascript and AJAX:
Put the following code in your get.html file. This will make an ajax request to your output.php file and put the response in a <div id="output"></div>.
<html>
<head></head>
<body>
<div id="output"></div>
<script>
var request = new XMLHttpRequest();
request.open('GET', 'output.php', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
document.getElementById('output').innerHTML = request.responseText;
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function() {
// There was a connection error of some sort
};
request.send();
</script>
</body>
</html>
I'm not 100% sure, if this is what you mean. But if you make a file (foo.php) and put it in the same directory as where output.php is, and then add this to foo.php:
<?php
$myfile = fopen("output.php", "r") or die("Unable to open file!");
echo fread($myfile,filesize("output.php"));
fclose($myfile);
?>
Then that should print the content of output.php in your browser, as HTML (if you go to /foo.php in the browser).
Is that it?

PHP writing a text file in the begin

So we are making in the class a sort of log. There is a input box and a button. Everytime the button is pressed, PHP will write on the text file and prints the current log. Now the text appears on the bottom, and we need to have the text appear on the top. Now how would we do that?
We tried doing this with alot of my classmates but it all resulted in weird behavours. (Like text is printed more then once, etc)
Thanks alot!
EDIT: Sorry, here is the code:
<html lang="en">
<head>
<title>php script</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="text"/>
<input type="submit" value="Submit" />
<?php
//Basic variables
echo("<br/>");
$myFile = "log.txt";
$logfile = fopen($myFile,'r+');
$theData = fread($logfile,filesize($myFile));
//Cookie stuff so the username is rememberd.
$username = $_COOKIE['gebruikerscookie'];;
if(isset($_POST['username'])){
$gebruiker = $_POST['username'];
if($_COOKIE['gebruikerscookie'] == $gebruiker){
$username = $_COOKIE['gebruikerscookie'];
echo("Welcome back");
}else{
setcookie("gebruikerscookie", $gebruiker);
$username = $_COOKIE['gebruikerscookie'];
echo("Welcome dude!");
}
}
//Checks if theres something inside
if(isset($_POST['text'])){
$message = "<br/>". $username ." : " . $_POST['text'];
fwrite($logfile, $message ,strlen($message));
}
echo($theData);
?>
</form>
</body>
Check the fopen manual on modes: http://www.php.net/manual/en/function.fopen.php
Try 'r+' Open for reading and writing; place the file pointer at the beginning of the file.
Altough without any code this is hard to answer.
<?php
$contentToWrite = "Put your log content here \n";
$contentToWrite .= file_get_contents('filename.log');
file_put_contents('filename.log', $file_data);
?>
This will add the previous content of your file after your cureent content and write on your file.
Please reply if you have any doubt.
you're just missing the
fclose();
I assume, since not closing a filehandle can cause a lot of strange errors like this.
So
$myFile = "log.txt";
$logfile = fopen($myFile,'r+');
........
//Checks if theres something inside
if(isset($_POST['text'])){
$message = "<br/>". $username ." : " . $_POST['text'];
fwrite($logfile, $message ,strlen($message));
}
fclose($logfile); // close it outside the if-condition!
echo($theData);
should do the trick
$log = file('log.txt');
krsort($log);
foreach($log as $line) echo "$line<br>\n";

Categories