actual I finished writing my program. Because it is only a plugin and it runs on a external server I still want to see if I get some errors or something else in the console.
I wrote every console input with echo ...;. My question now is if it is possible to get the text of the console?
Because then I could easily safe it in a .txt file and could get access to it from the web :) - Or is there another way to get the console text?
I could probably just say fwrite(...) instand of echo ...;. But this will cost a lot of time...
Greetings and Thank You!
An alternative that could be usefull on windows would be to save all the output buffer to a txt, first check your php configuration for the console app implicit_flush must be off then
<?php
ob_start(); //before any echo
/** YOUR CODE HERE **/
$output = ob_get_contents(); //this variable has all the echoes
file_put_contents('c:\whatever.txt',$output);
ob_flush(); //shows the echoes on console
?>
If your goal is to create a text file to access, then you should create a text file directly.
(do this instead of echoing to console)
$output = $consoleData . "\n";
$output .= $moreConsoleData . "\n";
(Once you've completed that, just create the file:)
$file = fopen('output.txt', 'a');
fwrite($file, $output);
fclose($file);
Of course, this is sparse - you should also check that the file exists, create it if necessary, etc.
For console (commando line interface) you can redirect the output of your script:
php yourscript.php > path-of-your-file.txt
If you haven't access to a command line interface or to edit the cronjob line, you can duplicate the starndar output at the begining of the script:
$fdout = fopen('path-to-your-script.txt', 'wb');
eio_dup2($fdout, STDOUT);
eio_event_loop();
fclose($fdout);
(eio is an pecl extension)
If you are running the script using the console (i.e. php yourscript.php), you can easily save the output my modifying your command to:
php yourscript.php > path/to/log.txt
The above command will capture all output by the script and save it to log.txt. Change the paths for your script / log as required.
Related
I'm currently creating a php page which does a ssh call with a .rb (ruby) file.
rb file
require 'metainspector'
page = MetaInspector.new("www.hln.be")
puts page.image
When creating a php file with the following code (php):
$cmd = "ruby facescrape.rb";
$last_line = system($cmd, $retval);
echo $last_line . '
echo $retval;
this only returns value 1.
However 2 things :
When running the same command in ssh, it will print the page.image
correctly.
When i change the rb file and for instance set as last line
puts "test"
this value returns correctly with also print correctly with the aboven php code.
I don't get why printing the page.image works in ssh but won't work by using that php code.
Also tried using exec() instead of system().
Thank you in advance!
Kind regards,
Kurt Colemonts
I'm working on a cron php script which will run once a day. Because it runs this way, the output from the file can't be seen.
I could literally write all the messages I want into a variable, appending constantly information I want to be written to file, but this would be very tedious and I have a hunch not necessary.
Is there a PHP command to tell the write buffer to write to a log file somewhere? Is there a way to get access to what has been sent to the buffer already so that I can see the messages my script makes.
For example lets say the script says
PHP:
<?
echo 'hello there';
echo 'hello world';
?>
It should output to a file saying: 'hello therehello world';
Any ideas? Is this possible?
I'm already aware of
file_put_contents('log.txt', 'some data', FILE_APPEND);
This is dependent upon 'some data', when I don't know what 'some data' is unless I put it in a variable. I'm trying to catch the results of whatever PHP has outputted.
You may want to redirect your output in crontab:
php /path/to/php/file.php >> log.txt
Or use PHP with, for example, file_put_contents():
file_put_contents('log.txt', 'some data', FILE_APPEND);
If you want to capture all PHP output, then use ob_ function, like:
ob_start();
/*
We're doing stuff..
stuff
...
and again
*/
$content = ob_get_contents();
ob_end_clean(); //here, output is cleaned. You may want to flush it with ob_end_flush()
file_put_contents('log.txt', $content, FILE_APPEND);
you can use ob_start() to store script output into buffer. See php documentation ob_get_clean
<?php
ob_start();
echo "Hello World";
$out = ob_get_clean();
$out = strtolower($out);
var_dump($out);
?>
If You're using cron I suppose that You run this on a Unix machine so:
One of approach is to write everything You want to stdout stream so in Unix You may grab this output to a file:
in php script:
$handle = fopen("php://stdout","w");
fwrite($handle,"Hello world"); // Hello world will be written to console
in cron job grab this output to a file:
#hourly php /var/www/phpscript.php >> /path/to/your/outputfile.txt
Notice: >> operator will append to a file and > operator will overwrite file by new data. File will be created automatically by first write
So everything you put to fwrite call as second argument will be placed in /path/to/your/outputfile.txt
You may call fwrite as many time as you want. Don't forget to close handler by fclose($handle);
This question already has answers here:
Save current page as HTML to server
(6 answers)
Closed 9 years ago.
I know this is outside of the ordinary use for PHP. I use PHP to generate templates for web front-ends. I then deliver these templates to a development team. They request that we deliver flat HTML files.
Is there a way to utilize PHP to save out the html version of the file. I have screen-02.php to screen-62.php. I have to individually open these in a browser and save the html as screen-02.html, screen-03.html , etc. I also have jQuery at my disposal if that helps.
Thanks in advance for any help.
using php output buffering? http://php.net/manual/en/function.ob-start.php
something like perhaps:
<?php
ob_start();
include_once("screen-01.php");
$content = ob_get_clean();
file_put_contents($fileName, $content);
?>
you could put in a loop to save all files at the same time as well, but depending on how many you should check the max execution time
I think it would be the easiest thing to write a Shell/Batch script and execute the PHP scripts from the CLI in stead of using them as a web page.
If you execute the following command:
php /some/page.php
You can generate the output that is wanted to your stdout, so if you use pipelining, you can easily do things like:
php /some/page.php >> /some/page.html
Or you can write a bash script (if you're on Linux) like this:
#!/bin/bash
for i in {1..5}
do
php /some/screen-$i.php >> /some/screen-$i.html
done
I think that will be the easiest (and fastest) approach, no other technologies are required.
If you don't have access to the PHP CLI, you can do a similar thing, but in stead of using the PHP CLI you can use wget to download the pages.
The easiest way (in my opinion) would be to use output buffering to store and then save the PHP output. You can use this without access to command-line server tools.
Create a new PHP file like so:
<?php
// Start output buffering
ob_start();
// Loop through each of the individual files
for ( $j = 0; $j<= 62; $j++ ){
ob_clean(); // Flush the output buffer
$k = str_pad( $j, 2, '0' ); // Add zeros if a single-digit number
require_once('screen-' . $k . '.php'); // Pull in your PHP file
if ( ob_get_length() > 0 ){ // If it put output into the buffer, process
$content = ob_get_contents(); // Pull buffer into $content
file_put_contents( 'screen-' $k . '.html', $content ); // Place $content into the HTML file
}
}
?>
And run it from the same server in the same path. Make sure the folder has write permissions (CHMOD) so it can create the new files. You should find it generates all your HTML files with the proper PHP output.
Maybe something like this:
<?php
$file = $_GET['file'];
$html = file_get_contents('http://yoururl.com/'.$file);
file_put_contents('./savedPages/'.$file.'.htm', $html);
?>
Call it with http://yoururl.com/savePage.php?file=yourtarget.php
When you use a templating engine like Smarty you can create the output and save it to a file without the need to display it in a browser.
$smarty = new Smarty;
$smarty->assign("variable", $variable);
$output = $smarty->fetch("templatefile.tpl");
file_put_contents("/path/to/filename.html", $output);
Smarty Documentation: http://www.smarty.net/docs/en/api.fetch.tpl
Another option would be to use the PHP outputbuffer
I have the following code
function generate_pdf() {
$fdf_data_strings = $this->get_hash_for_pdf();
#$fdf_data_names = array('49a' => "yes");
$fdf_data_names = array();
$fields_hidden = array();
$fields_readonly = array();
$hud_pdf = ABSPATH.'../pdf/HUD3.pdf';
$fdf= forge_fdf( '',
$fdf_data_strings,
$fdf_data_names,
$fields_hidden,
$fields_readonly );
/* echo "<pre>";
print_r($fdf);
echo "</pre>";
die('');
*/
$fdf_fn= tempnam( '.', 'fdf' );
$fp= fopen( $fdf_fn, 'w' );
if( $fp ) {
fwrite( $fp, $fdf );
//$data=fread( $fp, $fdf );
// echo $data;
fclose( $fp );
header( 'Content-type: application/pdf' );
header( 'Content-disposition: attachment; filename=settlement.pdf' ); // prompt to save to disk
passthru( 'pdftk HUD3.pdf fill_form '. $fdf_fn.' output - flatten');
unlink( $fdf_fn ); // delete temp file
}
else { // error
echo 'Error: unable to open temp file for writing fdf data: '. $fdf_fn;
}
}
}
is there anything wrong with it?
the problem is, I have installed pdftk
runing whereis pdftk gives me '/usr/local/bin/pdftk'
physically checked the location, pdftk is there at the said location..
using terminal, if i run pdftk --version or any other command, it runs
if I use php like passthru('/usr/local/bin/pdftk --version') nothing is displayed
if I used php like system("PATH=/usr/local/bin && pdftk --version"); it says '/usr/local/bin /pdftk :there is no directory of file '
when I run this function script , prompt for file download pops, but when i save it, nothng is saved,
i have checked permission for this folder and changed it 0755, 0766, 0777, 0666 i have tried all, nothng works
For 3 days, i am striving to get over it, and I have asked question regarding this too, but Can't figure out what the hell is going on with me.
Can somebody help me before i strike my head with wall?
The pasthru function does not execute the program through the shell.
Pass the exact path into the passthru command.
E.g.
passthru( '/usr/local/bin/pdftk HUD3.pdf fill_form '. $fdf_fn.' output - flatten');
or
passthru( '/usr/local/bin/pdftk' . $hud_pdf . 'fill_form '. $fdf_fn.' output - flatten');
If this still doesn't work test using
<?php passthru("/path/to/pdftk --help"); ?> where /path/to/pdftk is your path returned by which or where is, to ensure path is correct.
If path is correct then the issue may be related to permissions either on the temporary directory you tell pdftk to use or the permissions on the pdftk binary with regards to the apache user.
If these permissions are fine you can verify the pdftk starts up from php but hangs from running your command, then might be able to try the workaround listed here.
Further documentation on passthru is avaliable passthru PHP Manual.
As a side note, the putenv php function is used to set environment variables.
E.g. putenv('PATH='.getenv('PATH').':.');
All 3 PHP functions: exec(), system() and passthru() executes an external command, but the differences are:
exec(): returns the last line of output from the command and flushes nothing.
shell_exec(): returns the entire output from the command and flushes nothing.
system(): returns the last line of output from the command and tries to flush the output buffer after each line of the output as it goes.
passthru(): returns nothing and passes the resulting output without interference to the browser, especially useful when the output is in binary format.
Also see PHP exec vs-system vs passthru SO Question.
The implementation of these functions is located at exec.c and uses popen.
I had the same issue and this is working after lots of experiments :
function InvokePDFtk($pdf_tpl, $xfdf,$output){
$params=" $pdf_tpl fill_form $xfdf output $output flatten 2>&1";
$pdftk_path=exec('/usr/bin/which /usr/local/bin/pdftk');
$have_pdftk= $pdftk_path=='/usr/local/bin/pdftk' ;
$pdftk_path=$have_pdftk ? $pdftk_path : 'pdftk ';
exec($pdftk_path.$params,$return_var);
return array('status'=> $have_pdftk,
'command' =>$pdftk_path.$params, 'output'=>$return_var);
}
hope this might give you some insight . (change according to your needs)
Completing Appleman answer, those 3 functions can be considered as dangerous, because they allow you execute program using php, thus an attacker that exploited one of your script if you are not careful enougth. So in many php configuration that want to be safe they are disabled.
So you should check for the disable_functions directive in you php.ini(and any php configuration file) and see if the function you use is disabled.
Perhaps you should keep fclose out of the if statement, make sure you have it directed to the right file! :)
Is your web server chrooted? Try putting the executable into a directory that is viewable by the server.
Play around around with safe mode and definitely check your web server log file, normally in:
/var/log/apache2/error.log
This is my code:
$zplHandle = fopen($target_file,'w');
fwrite($zplHandle, $zplBlock01);
fwrite($zplHandle, $zplBlock02);
fwrite($zplHandle, $zplBlock03);
fclose($zplHandle);
When will the file be saved? Is it immediately after writing to it or after closing it?
I am asking this because I have Printfil listening to files in a folder and prints any file that is newly created. If PHP commits a save immediately after fwrite, I may run into issues of Printfil not capturing the subsequent writes.
Thank you for the help.
PHP may or may not write the content immediately. There is a caching layer in between. You can force it to write using fflush(), but you can't force it to wait unless you use only one fwrite().
I made a tiny piece of code to test it and it seems that after fwrite the new content will be detected immediately,not after fclose.
Here's my test on Linux.
#!/usr/bin/env php
<?php
$f = fopen("file.txt","a+");
for($i=0;$i<10;$i++)
{
sleep(1);
fwrite($f,"something\n");
echo $i," write ...\n";
}
fclose($f);
echo "stop write";
?>
After running the PHP script ,I use tail -f file.txt to detect the new content.And It shows new contents the same time as php's output tag.
the file will be saved on fclose. if you want to put the content to the file before, use fflush().
Assuming your working in PHP 5.x, try file_put_contents() instead, as it wraps the open/write/close into one call.
http://us3.php.net/manual/en/function.file-put-contents.php