Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

PHP Cursor Positioning

I was wondering if there was a way to move the cursor after opening a file using:

Code
fopen("file", "a");

Such that the cursor is two lines up.
PHP Code

<?php 
$content
=file("file.txt");
array_splice($content, count($content)-2);
$content=implode(PHP_EOL, $content).$whatyouwantoaddtothefile;
$x=fopen("file.txt", "w");
fwrite($x, $content);
fclose($x); 
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.
Thanks a lot, but unfortunately it didnt work.
(Apr 11, 2019 01:48 PM)Jack Drake Wrote:  Thanks a lot, but unfortunately it didnt work.

I guess what you want is that lines are always being added on top of file.

That you can achieve like this:
PHP Code

<?php 
$file 
= 'test.txt';
$fh = fopen($file, 'a');
fwrite($fh, "\n test string");
fclose($fh); 
The issue is that the file everything is appended from the bottom and it needs to be above the first bracket for json.
(Apr 11, 2019 02:05 PM)Jack Drake Wrote:  The issue is that the file everything is appended from the bottom and it needs to be above the first bracket for json.

You should show us some more code to be able to help you properly.
The file is in json and it has information that can't be posted here, but the php file is appending information into the file. Now, the issue is that when whenever i append data it adds it into the file at the end

Code
{
  JSON DATA
}
appended information is out here

It doesn't put the info inside the curly braces, so that's why I want to move the cursor up 2 lines to append it there.
(Apr 11, 2019 04:09 PM)Jack Drake Wrote:  The file is in json and it has information that can't be posted here, but the php file is appending information into the file. Now, the issue is that when whenever i append data it adds it into the file at the end

Code
{
 JSON DATA
}
appended information is out here

It doesn't put the info inside the curly braces, so that's why I want to move the cursor up 2 lines to append it there.

First retrieve the content of your file (file_get_contents) & store it in a variable ($content)
json_decode its value, add your wanted datas to your array
json_encode & put the content back to it's file.

PHP Code

<?php 
$content 
= json_decode(file_get_contents("/path/to/file")); // Retrieve json encoded datas, decode it, store it in $content
$content['new_key'] = "new value"; // Adding new stuff 

file_put_contents('/path/to.file', json_encode($content); // Saving it 
[Image: vibesignature.gif]
“Whatever Your Mind Can Conceive and Believe, It Can Achieve.” – Napoleon Hill
Thanks a lot it worked out when i got the file content.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by DopeishApr 24, 2017 03:28 PM 7 223 Apr 24, 2017 03:28 PM
Last Post: Nachfolger
[PHP] by blackknight_Aug 2, 2014 08:35 AM 1 91 Aug 2, 2014 08:35 AM
Last Post: AppleJuice
[Javascript]  by Melkor333Jun 23, 2014 12:37 PM 2 93 Jun 23, 2014 12:37 PM
Last Post: Melkor333