fprint doens't work in my php - php

I want to write some text from script in browser.
<?php
$out = fopen('php://stdout', 'w');
fprintf($out, "Hello!");
fclose($out);
?>
I expect "Hello!" on output, but nothing happens...Could it be bug in stdout, or I just oversighted something?

This would be true if calling php from console.
But if you are talking about web access, you should use php://output

Have you tried the simple :
<?php
$out = "foobar";
echo $out;
?>
?
By default, php print on STDOUT.

Related

Write XHR data with line break

I get some data from a variable, and send it via XHR to PHP file which write it in to a file.
How can I make a line break in the file after every write to it?
var e='Hello world!'
$.getJSON('http://example.com/write.php?callback=&data='+e);
write.php:
$data = $_GET['data'];
if(!$op=fopen('my file name','ab')){
$res='b';
exit;
}
if(fwrite($op,$data)===FALSE){
$res='b';
exit;
}
fclose($op);
Try writing or appending a PHP_EOL to the file after you have written your data.
fwrite($op, PHP_EOL);
Try:
$data = $_GET['data'];
if(!$op=fopen('my file name','ab')){
$res='b';
exit;
}
if(fwrite($op,$data . PHP_EOL)===FALSE){
$res='b';
exit;
}
fclose($op);
AFAIK, fwrite() will automatically append a newline for each write.
Are you running PHP on a Windows server? You might try to open the file to write to in 'at' mode (append, translate line-endings) in stead of 'binary' mode (ab);
$op = fopen('my file name','at');
http://php.net/manual/en/function.fopen.php

Write terminal output results to file in php

I have run my commands using system and i would like for it copy the terminal results to a file called output.txt. When I run my php script on the browser it displays the result but not in the output.txt the file is empty.
echo '<pre>';
$last_line = system('ruby /home/simon/ruby-grok/examples/result.rb', $retval);
echo '</pre>';
$out = fopen('output.txt', 'w'); //output handler
fputs($out,$last_line); //writing output operation
fclose($out);
Code seems fine.
Does script have the permission to write in that specific file? And can you try instead of
$out = fopen('output.txt', 'w'); //output handler
fputs($out,$last_line); //writing output operation
fclose($out);
to put this:
file_put_contents('output.txt',$last_line);
edit:
btw i forgot to ask. what output your browser displays? is there any data in $out variable?
Using backticks is much better.
$variable = `ls -l`;
and then:
$out = fopen('output.txt', 'w'); //output handler
fputs($out,$variable); //writing output operation
fclose($out);
or:
file_put_content('output.txt',$variable);

Display remote text file on php page

I'm using this code now to display a text file on a php/html page.
<?php
foreach (glob("example.txt") as $filename) {
echo nl2br(file_get_contents($filename));
echo "<br></br>";
}
?>
I'm looking for a way to display the example.txt file from another server with URI.
Something like this: http://address.com/dir/example.txt
Is there a simple way to do this?
(I would use an iframe but it's not possible to style the text without Java or JQuery).
You could just use
file_get_contents('http://address.com/dir/example.txt');
You code is totally wrong
foreach (glob("example.txt") as $filename) {
^------------------------- searching for a file
They can only one example.txt file in a folder at a time except you want to get all text files should should be like this in the first place
foreach (glob("*.txt") as $filename) {
If that is not the case the code would work for both remote and local file
error_reporting(E_ALL);
ini_set("display_errors", "on");
$fileName = "example.txt" ; // or http://oursite.com/example.txt
echo nl2br(file_get_contents($fileName));
echo "<br></br>";
You will have to use CURL to fetch the content of the file first and then display it.
Another option is to use iframes and set the target of iframe to the desired text file.
Yet another option is to use ajax to fetch the content from client end as suggested in comment.
Check fopen() / fread() and your available transport wrappers.
For normal length text file you can use:
<?PHP
$file = fopen("http://www.xyz.com/textfile.txt", "rb");
$output = fread($file, 8192);
fclose($file);
echo($output);
echo "<br>";
?>
For longer files:
<?PHP
$file = fopen("http://www.xyz.com/textfile.txt", "rb");
$output = '';
while (!feof($file)) {
$output .= fread($file, 8192);
}
fclose($file);
echo($output);
echo "<br>";
?>
It will prevent packet exceed issues in longer files by concatenating the file together in several groupings using while loop

shell_exec in PHP returns empty string

shell_exec and exec are not returning any content. I can't figure out what's wrong.
Here's some code:
echo 'test: ';
$output = shell_exec('whoami');
var_export($output, TRUE);
echo PHP_EOL . '<br>' . PHP_EOL;
And here's the source of the output
test 2:
<br>
I do not have control over the host, but I believe they're running SuPHP. According to phpinfo, safe_mode is off. Running whoami from SSH outputs the expected value.
I'm at a loss. Any idea how to debug this?
You're never printing the $output variable. The var_export() call returns the content of the variable when you call it with a true second parameter, it does not print it directly.
If you want the output from a shell command read back into PHP, you're probably going to need popen(). For example:
if( ($fp = popen("some shell command", "r")) ) {
while( !feof($fp) ) {
echo fread($fp, 1024);
flush(); // input will be buffered
}
fclose($fp);
}

Save website sourecode to file via php

Hi I wan to save the sourecode of http://stats.pingdom.com/w984f0uw0rey to some directory in my website
<?php
if(!copy("http://stats.pingdom.com/w984f0uw0rey", "stats.html"))
{
echo("failed to copy file");
}
;
?>
but this does not work either for me:
<?php
$homepage = file_get_contents('http://stats.pingdom.com/w984f0uw0rey');
echo $homepage;
?>
But I cannot figure how to do it!
thanks
use
<?
file_put_contents('w984f0uw0rey.html', file_get_contents('http://stats.pingdom.com/w984f0uw0rey'));
?>
be sure that the script has write privileges to the current directory
Use file_get_contents().
The best variant you can do in PHP is to use stream_copy_to_stream:
$url = 'http://www.example.com/file.zip';
$file = "/downloads/stats.html";
$src = fopen($url, 'r');
$dest = fopen($file, 'w');
echo stream_copy_to_stream($src, $dest) . " bytes copied.\n";
If you need to add HTTP options like headers, use context options with the fopen call. See as well this similar answer which shows how. It's likely you need to set a user-agent and things so that the other website's server believes you're a browser.

Categories