How to save html output to .java file from php - php

This is my html output. from .php file
public class Resource {
public static int cliamer = fgfgfger;
public static int reporting = grgrging;
}
I want to save this to mytext.java how is it possible with php

Something like this:
$myData = "java code goes here";
$fp = fopen('dir/mytext.java', 'w');
fwrite($fp, $myData);
fclose($fp);
Main point of failure here will be the folder's permission.

// beginning of your script
ob_start();
// your script
// end of your script
$data = ob_get_contents();
ob_end_clean(); // or ob_end_flush(); if you also want to output something
file_put_contents(dirname(__FILE__).'/mytext.java', $data);

Related

How to copy the source html file and save it after replacing string to another directory using php?

$a = trim($allDataInSheet [$i]["A"]);
$b=trim($allDataInSheet [$i]["B"]);
/* copy the source file */
$fname = "edm.html";
copy("edm.html","hk/edm.html");
$fcopy="hk/edm.html";
/* read the duplicate file */
$fhandle = fopen($fcopy,"r");
$content = fread($fhandle,filesize($fcopy));
/* replace the string in the duplicate file */
$content = str_replace($a,$b, $content);
$fhandle = fopen($fcopy,"w");
fwrite($fhandle,$content);
fclose($fhandle);
Above code was not working properly,I dont want to replace the string inside the source file.I need to take the duplicate of the source file and change the strings.Thanks in advance.Pleaes help me to fix it..
I think this may be simpler if you just use file_get_contents and file_put_contents.
$fname = 'edm.html';
copy('edm.html', 'hk/edm.html');
$fcopy = 'hk/edm.html';
// read the file
$content = file_get_contents($fcopy);
// make the replacement
$content = str_replace($a, $b, $content);
// write the file
file_put_contents($fcopy, $content);
Also if you put
error_reporting(E_ALL);
ini_set('display_errors', 1);
at the start of your script you will make sure that you are seeing all PHP errors.

How to get include contents as a string? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Execute a PHP file, and return the result as a string
PHP capture print/require output in variable
I am trying to get the contents of an include to a string. Is that possible?
For example, if I have a test.php file:
echo 'a is equal to '.$a;
I need a function, say include_to_string to include the test.php and return what would be output by in in a string.
Something like:
$a = 4;
$string = include_to_string(test.php); // $string = "a is equal to 4"
ob_start();
include 'test.php';
$string = ob_get_clean();
I think is what you want. See output buffering.
ob_start();
include($file);
$contents = ob_get_contents(); // data is now in here
ob_end_clean();
You can do this with output buffering:
function include2string($file) {
ob_start();
include($file);
return ob_get_clean();
}
#DaveRandom points out (correctly) that the issue with wrapping this in a function is that your script ($file) will not have access to variable defined globally. That might not be an issue for many scripts included dynamically, but if it is an issue for you then this technique can be used (as others have shown) outside of a function wrapper.
** Importing variables
One thing you can do is to add a set of data you would like to expose to your script as variables. Think of it like passing data to a template.
function include2string($file, array $vars = array()) {
extract($vars);
ob_start();
include($file);
return ob_get_clean();
}
You would call it this way:
include2string('foo.php', array('key' => 'value', 'varibleName' => $variableName));
and now $key and $variableName would be visible inside your foo.php file.
You could also provide a list of global variables to "import" for your script if that seems clearer to you.
function include2string($file, array $import = array()) {
extract(array_intersect_key($GLOBALS, array_fill_keys($import, 1)));
ob_start();
include($file);
return ob_get_clean();
}
And you would call it, providing a list of the globals you would like exposed to the script:
$foo='bar';
$boo='far';
include2string('foo.php', array('foo'));
foo.php should be able to see foo, but not boo.
You could also use this below but I recommend the above answer.
// How 1th
$File = 'filepath';
$Content = file_get_contents($File);
echo $Content;
// How 2th
function FileGetContents($File){
if(!file_exists($File)){
return null;
}
$Content = file_get_contents($File);
return $Content;
}
$FileContent = FileGetContents('filepath');
echo $FileContent;
Function in PHP manual : file_get_contents

Use php://temp wrapper with XMLWriter

Is it possible to use the php://temp wrapper to generate an XML file with XMLWriter? I like the features it provides (memory for small files, transparent temporary file for larger output) but I can't get the syntax (if it's even possible):
<?php
header('Content-type: text/xml; charset=UTF-8');
$oXMLWriter = new XMLWriter;
$oXMLWriter->openURI('php://temp');
$oXMLWriter->startDocument('1.0', 'UTF-8');
$oXMLWriter->startElement('test');
$oXMLWriter->text('Hello, World!');
$oXMLWriter->endElement();
$oXMLWriter->endDocument();
// And now? *******
$oXMLWriter->flush();
I don't understand the purpose of writing to a temp file. Perhaps you want:
$oXMLWriter->openURI('php://output');
I haven't ever used XMLWriter but it doesn't seem to take a handle to a file pointer. I think that's really what you want.
For giggles, here's something that wraps the temp interface:
class WeirdStream
{
static public $files = array();
private $fp;
public function stream_open($path)
{
$url = parse_url($path);
self::$files[$url['host']] = fopen('php://temp', 'rw');
$this->fp = &self::$files[$url['host']];
return true;
}
public function stream_write($data)
{
return fwrite($this->fp, $data);
}
}
stream_wrapper_register('weird', 'WeirdStream');
$oXMLWriter = new XMLWriter;
$oXMLWriter->openURI('weird://a');
// .. do stuff
$oXMLWriter->flush();
Now you can get at the file pointer:
$fp = WeirdStream::$files['a'];
It may be purely in memory, or it may be a temporary file on disk.
You could then loop through the data line by line:
fseek($fp, 0, SEEK_SET);
while (!feof($fp)) $line = fgets($fp);
But this is all very odd to me.
What do you need to do with the contents of php://temp eventually? If you just need a temporary, memory-only storage, then you can use openMemory():
$oXMLWriter = new XMLWriter;
$oXMLWriter->openMemory();
$oXMLWriter->startDocument('1.0', 'UTF-8');
$oXMLWriter->startElement('test');
$oXMLWriter->text('Hello, World!');
$oXMLWriter->endElement();
$oXMLWriter->endDocument();
echo $oXMLWriter->outputMemory ();

PHP output buffering to text file

I am having trouble with an update script. It runs for a few hours so I would like it to output live to a text file.
I start the document with
ob_start();
Then within the while loop (as it iterates through the records of the database) I have this
$size=ob_get_length();
if ($size > 0)
{
$content = ob_get_contents();
logit($contents);
ob_clean();
}
And finally the logit function
function logit($data)
{
file_put_contents('log.txt', $data, FILE_APPEND);
}
However the log file remains empty. What am I doing wrong?
try
logit($content);
// ^^ Note the missing s
$contents is not the same variable as $content.
For anyone coming here looking for a function for this, I wrote a nice one today:
//buffer php outout between consecutive calls and optionally store it to a file:
function buffer( $toFilePath=0, $appendToFile=0 ){
$status = ob_get_status ();
if($status['level']===1) return ob_start(); //start the buffer
$res = ob_get_contents();
ob_end_clean();
if($toFilePath) file_put_contents($toFilePath, $res, ($appendToFile ? FILE_APPEND : null));
return $res;
}
Sample usage:
buffer(); //start the buffer
echo(12345); //log some stuff
echo(678910);
$log = buffer('mylog.txt',1); //add these lines to a file (optional)
echo('Heres the latest log:'.$log);

PHP - print content from file after manipulation

I'm struggling trying to read a php file inside a php and do some manipulation..after that have the content as a string, but when I try to output that with echo or print all the php tags are literally included on the file.
so here is my code:
function compilePage($page,$path){
$contents = array();
$menu = getMenuFor($page);
$file = file_get_contents($path);
array_push($contents,$menu);
array_push($contents,$file);
return implode("\n",$contents);
}
and this will return a string like
<div id="content>
<h2>Here is my title</h2>
<p><? echo "my body text"; ?></p>
</div>
but this will print exactly the content above not compiling the php on it.
So, how can I render this "compilePage" making sure it returns a compiled php result and not just a plain text?
Thanks in advance
function compilePage($page, $path) {
$contents = getMenuFor($page);
ob_start();
include $path;
$contents .= "\n".ob_get_clean();
return $contents;
}
To evaluate PHP code in a string you use the eval function, but this comes highly unadvised. If you have a file containing PHP code, you can evaluate it with include, include_once, require, or require_once depending on your need. To capture the output of an included file - or required, or whichever method - you need to enable output buffering.
You can use output buffering for this, and include the file normally:
function compilePage($page,$path){
$contents = array();
$menu = getMenuFor($page);
ob_start();
include $path;
$file = ob_get_contents();
ob_end_clean();
array_push($contents,$menu);
array_push($contents,$file);
return implode("\n",$contents);
}
The include() call will include the PHP file normally, and <?php blocks will be parsed and executed. Any output will be captured by the buffer created with ob_start(), and you can get it later with the other ob_* functions.
You need to use include() so it will execute. You can couple this with output buffering to get the return in a string.
function compilePage($page,$path){
$contents = array();
$menu = getMenuFor($page);
//output buffer
ob_start();
include($path);
$file = ob_get_contents();
ob_end_clean();
array_push($contents,$menu);
array_push($contents,$file);
return implode("\n",$contents);
}

Categories