Hack Forums
[PHP] PHP Cursor Positioning - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] PHP Cursor Positioning (/showthread.php?tid=5959330)



PHP Cursor Positioning - Jack Drake - Apr 11, 2019

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.


RE: PHP Cursor Positioning - Squito - Apr 11, 2019

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



RE: PHP Cursor Positioning - Jack Drake - Apr 11, 2019

Thanks a lot, but unfortunately it didnt work.


RE: PHP Cursor Positioning - ChaoticHaze - Apr 11, 2019

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



RE: PHP Cursor Positioning - Jack Drake - Apr 11, 2019

The issue is that the file everything is appended from the bottom and it needs to be above the first bracket for json.


RE: PHP Cursor Positioning - ChaoticHaze - Apr 11, 2019

(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.


RE: PHP Cursor Positioning - Jack Drake - Apr 11, 2019

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.


RE: PHP Cursor Positioning - newQuery - Apr 12, 2019

(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 



RE: PHP Cursor Positioning - Jack Drake - Apr 14, 2019

Thanks a lot it worked out when i got the file content.