Php, Windows, how to capture what a running batch file doing? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Since it runs long, and prints a lot of information, I dont want to wait until it has run, I want to capture its content while its running. Is it possible? If I could do it with exec() it would wait until its over

You can try it with popen. Example:
<?php
$command = ('ping stackoverflow.com');
$handle = popen ($command,'r');
while (!feof($handle))
{
$buffer = fgets($handle, 4096);
echo $buffer;
}
pclose($handle);
?>
In a Windows-batchfile don't forget the exit (http://de3.php.net/manual/en/function.popen.php#77723).

Related

PHP - Trying to delete some elements of an array then repost all the content to a file. [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
if(isset($_POST["delete"])){
$file_name = 'ids';
touch($file_name);
$handle = fopen($file_name, 'r+');
$stream = file_get_contents('ids');
$pieces = explode(" ", $stream);
unset($pieces[0]);
unset($pieces[1]);
unset($pieces[2]);
file_put_contents("ids", "\n" );
file_put_contents("ids", $pieces);
fclose($handle);
}
So I'm sure this is wicked messy and a terrible way to accomplish my goal but I am learning PHP for school and this is giving me a lot of issues.
When I press my button I want to take the content I have in 'ids', change it by deleting the strings located in the array at 0,1,2 and then rewrite the new content to 'ids'. Any help would be appreciated.
I'm not sure where you are getting stuck, but I would guess it is regarding the "re-assembly" process, so I'll fix that.
Untested Code:
if(isset($_POST["delete"])){
$file_name='ids';
touch($file_name);
$handle=fopen($file_name, 'r+');
$stream=file_get_contents($file_name);
$content=str_replace(" ","\n",explode(" ", $stream,4)[3]); // explode into a max of 4 elements, access the last element, replace spaces with `\n`
file_put_contents($file_name,$content);
fclose($handle);
}

In PHP fopen function, what's the difference between w and w+ mode? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have read the manaul, and knew that w+ write and read, w only write. But how to understand it?
$file_name = $_SERVER['DOCUMENT_ROOT']."/../data/test.txt"; //file is exist
$fp = fopen($file_name, 'w+');
fwrite($fp, 'hello, world!');
$len = filesize($file_name);
echo $len; //13
$contents = fread($fp, $len);
var_dump($contents);//string(0) "" why?
fclose($fp);
Both options w and w+ truncates file during opening with fopen. Difference is that you can read exactly that was written during current session working with file(I mean fopen->fwrite->fread(will not empty)->fclose). So if you try to read file opened with w and w+ = the result in both case should be empty

How to open two files concurrently with fopen ? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I wrote a php script that reads a log file using fopen and extracts all necessary info.
The issue is that i need to extract the info as an array concurrently from two files.
How do i do that with fopen ?
You can have as many file pointers are required. You can even call fopen in loop like
<?php
$files = array("file1","file2","file3");
$fps = array();
foreach($files as $fls)
$fps[] = fopen($fls);
///rest of the code.
?>
fopen returns a pointer to the file.
Just call fopen and fread twice:
$filename1 = "file1.log";
$handle1 = fopen($filename1);
$content1 = fread($handle1, filesize($filename1));
fclose($handle1);
$filename2 = "file2.log";
$handle2 = fopen($filename2);
$content2 = fread($handle2, filesize($filename2));
fclose($handle2);

File Writing in php without appending [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Following is my piece of code which writes to file.
<?php
$fileWrite = fopen("c.txt", "w+");
for($i=0;$i<5;$i++) {
$bytes = fwrite($fileWrite, $i);
}
fclose($fileWrite);
I am getting 01234. It means , pointer is appending to last location, I don't want to append data. Instead need to write 4 in the file.
Then you need to ftruncate the file before writing to it:
ftruncate($fileWrite, 0);
$bytes = fwrite($fileWrite, $i);
This is obviously pretty pointless to do in a loop, but I expect you know that.
I personally recommend using file_put_contents for this simple task. It's easier to use and will not append to the end of the file (unless specified that way).

Find a specific line and replace it with a new one? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a .php file that contains array data like below:
$config['setting_one'] = true;
$config['setting_two'] = "some string";
$config['setting_three']['option1'] = "some string";
What is the correct way to find the line starts with $config['setting_two'] and replace the line with $config['setting_two'] = "new string"; (new string will come from a post value).
There is no correct way.
The correct way is "don't do that".
You include the php code, and then update the variable by simply setting it.
What are you trying to do?
You will have to read the file line by line using fgets
$config = array();
while(($line = fgets($handle, 4096) ) !== false) {
$config[] = $line;
}

Categories