File Writing in php without appending [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
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).

Related

Content of File PHP [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 1 year ago.
Improve this question
I have a file that i opened:
$linie = file("hasla-kopia.txt"); (It's in Polish)
And i have a problem with getting content of a file where every element is in the next line.
This is how my file looks like:
enter image description here
I want the exact output on my php file. I know you can do it by nl2br() function but it's really important for me to make every element in a different HTML tag because i want my final output to be:
1000111011111 in decimal system is: some number
and I don't know how to do the content on separate lines, change to int and write this text after it. Can someone please help me out?
$lines = file('hasla-kopia.txt');
foreach ($lines as $line_num => $line) {
echo $line."<br>";
}
that allows reading line by line and each line is the variable $line
you can use
$file = file_get_contents("hasla-kopia.txt");
$em = explode("\n" , $file);
for($i=0;$i<count($em);$i++){
echo $em[$i];
};

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);
}

PHP Replace binary file at hex address [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 7 years ago.
Improve this question
I want to modify a binary file server side of aprox. 700kb to change an url in it than save it.
My first idea was to use bin to hex and preg_replace to replace url in binary.
The adress for the url in binary is always the same, but i need to change it every time my function is called.
Is there a better/faster way to do this ?
Example:
somesite.com/api***
I want to replace *** with some numbers from a var for example.
*** is between hex adress 00010edb-00010edd
Thanks!
If the string is at fixed position, you can write data directly:
$position=hexdec("00010edb"); // You have to pre-calculate it once
$data="some data"; // Replacement
if ($f=fopen("your_file", "r+")) {
fseek($f, $position);
fwrite($f, $data);
fclose($f);
} else {
echo "Can't open file";
}

Php, Windows, how to capture what a running batch file doing? [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
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).

How to retrieve a random line from text file, then remove the line from the text file in PHP [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 am having a bit of a hard time thinking about what to do for this. I would like to display a random line from a specific text file, then after it displays it will remove that line from the text file so it will no longer show up when page refreshed.
$lines = file('');
echo $lines[array_rand($lines)];
file read lines into array. array_rand to get a random key from the array of lines. unset to remove an element. file_put_contents to write back to file.
$filename = 'filename.txt';
$lines = file($filename);
$r = array_rand($lines);
echo $lines[$r];
unset($lines[$r]);
file_put_contents($filename, implode("", $lines));

Categories