file_put_contents -> how to save file from extension (debug purpose)

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
mpaw
Registered User
Posts: 141
Joined: Mon Sep 16, 2019 1:09 pm

file_put_contents -> how to save file from extension (debug purpose)

Post by mpaw »

Hi.

I'm trying to save file from extension level by calling

Code: Select all

public function viewtopic_modify_post_data($event)
    {
        $post_list = $event['post_list'];
        $post_list = array_slice($post_list, 0, 1);
        $event['post_list'] = $post_list;
    
        file_put_contents('http://localhost/phpBB3/my_path/test.txt', json_encode($post_list), FILE_APPEND | LOCK_EX);
    }
the dir and file has chmod 777. But maybe http protocol does not give permission to save file by it. How to save file?

BTW. Then instruction of modification post_list array doesn't work.

Thanks
Mike
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 28619
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: file_put_contents -> how to save file from extension (debug purpose)

Post by Paul »

Did you try reading the php documention? For example https://www.php.net/file_put_contents
This has nothing to do with phpBB or extensions. You should do a bit of research before posting.
mpaw
Registered User
Posts: 141
Joined: Mon Sep 16, 2019 1:09 pm

Re: file_put_contents -> how to save file from extension (debug purpose)

Post by mpaw »

I did. But phpBB when calls listener its directory is unknown by me. It isn't /event/ dir. I don't know how to specify path, without using http protocol

PS.

Ok, I've specified absolute path:

Code: Select all

/var/www/html/phpBB3/ext/my_name/my_ext/my_dir/my_file.txt
But it still doesn't work :( I've given 777 chmod

Code: Select all

public static function getSubscribedEvents()
{
	return [
		'core.user_setup'							=> 'load_language_on_setup',
		'core.display_forums_modify_template_vars'	=> 'display_forums_modify_template_vars',
		'core.posting_modify_message_text'          => 'testFun',
		'core.viewtopic_modify_post_data'	        => 'viewtopic_modify_post_data'
	];
}

public function viewtopic_modify_post_data($event)
{
    $post_list = $event['post_list'];
    $post_list = array_slice($post_list, 0, 1);
    $event['post_list'] = $post_list;

    file_put_contents('/var/www/html/phpBB3/path_to_file/test.txt', json_encode($post_list), FILE_APPEND | LOCK_EX);
}
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: file_put_contents -> how to save file from extension (debug purpose)

Post by 3Di »

Instead of asking a thousand questions for everything, you should first ask for an extension, so that someone interested can write it down for you. Nobody has figured out what you really want to do and if anything it is possible, so trying blindly does not bring the shopping home.
---------------------------------------
Anyway, for what it's worth, try it.

Code: Select all

    $post_list = $event['post_list'];
    $post_list = array_slice($post_list, 0, 1);
    file_put_contents('./my_path/test.txt', json_encode($post_list), FILE_APPEND | LOCK_EX);
    $event['post_list'] = $post_list;
BTW you can simply dump the variable to the screen with var_dump() or print_r()
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
mpaw
Registered User
Posts: 141
Joined: Mon Sep 16, 2019 1:09 pm

Re: file_put_contents -> how to save file from extension (debug purpose)

Post by mpaw »

Thank you. Where does point

Code: Select all

'./'
?
Cause not to event dir, where listener is located...

Btw. This event doesn't work. How to modify viewed post from topic, without modifying db?

Tell me please what shall I ask and how to get support?
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: file_put_contents -> how to save file from extension (debug purpose)

Post by 3Di »

mpaw wrote: Thu Sep 23, 2021 11:58 am Thank you. Where does point ./
That should be your phpBB root path. so phpBB3/my_path/test.txt
mpaw wrote: Thu Sep 23, 2021 11:58 am This event doesn't work.
It is your code that does not work for what you are supposed to do with it, which nobody knows.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
mpaw
Registered User
Posts: 141
Joined: Mon Sep 16, 2019 1:09 pm

Re: file_put_contents -> how to save file from extension (debug purpose)

Post by mpaw »

3Di wrote: Thu Sep 23, 2021 12:08 pm It is your code that does not work for what you are supposed to do with it, which nobody knows.
I want to modify viewed posts when choosen topic is displayed. In that case i want to show only 1st post from topic. (To test event for working properly)
User avatar
Dark❶
Registered User
Posts: 395
Joined: Mon Jan 15, 2018 1:22 pm
Location: D@rK V0id
Name: Dark❶ [dark1]
Contact:

Re: file_put_contents -> how to save file from extension (debug purpose)

Post by Dark❶ »

mpaw wrote: Thu Sep 23, 2021 12:20 pm ...
I want to modify viewed posts when choosen topic is displayed. In that case i want to show only 1st post from topic. (To test event for working properly)
Try var_dump(...) (https://www.php.net/manual/en/function.var-dump.php) first,
check if it works in your function block.

if it doesn't then that means your function block does not execute,
figure that out by your-self since your whole Ext code is not provided here.
I suggest you check this location for proper configuration : <phpBB_root>/ext/<Your_Vendor>/<Your_Ext>/config/services.yml
I can not help you more than this, due to lack of code.

If the var_dump(...) works, then try your code like this:

Code: Select all

file_put_contents(realpath(__DIR__).'/test.txt', json_encode($post_list));
I hope this helps.

Best regards :+1:
Dark❶ [dark1]
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: file_put_contents -> how to save file from extension (debug purpose)

Post by 3Di »

Try this - then look into your store folder and you will see your text.

Code: Select all

    global $phpbb_root_path;
    $post_list = $event['post_list'];
    $post_list = array_slice($post_list, 0, 1);
    file_put_contents($phpbb_root_path . 'store/test.txt', json_encode($post_list), FILE_APPEND | LOCK_EX);
    $event['post_list'] = $post_list;
BTW the post_list only contains the array with post_ids, only numbers.
Attachments
file_put_contents_event.png
file_put_contents_event.png (6.15 KiB) Viewed 1864 times
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
mpaw
Registered User
Posts: 141
Joined: Mon Sep 16, 2019 1:09 pm

Re: file_put_contents -> how to save file from extension (debug purpose)

Post by mpaw »

Thank you so much, 3Di, now I have tool to debug :) Thanks!

I've figured out, that posts encoded messages are in

Code: Select all

$event['rowset']['id_number']['post_text']
But post_text is in like a raw data from db. Is there any tool to parse it into post_data like in post editor? And similar tool to do same thing but in second way? I can ofcourse modify raw post_text, but it will be easier to work on parsed message.

Thanks
Mike
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: file_put_contents -> how to save file from extension (debug purpose)

Post by 3Di »

To UNPARSE use \phpbb\textformatter\s9e\utils - Ie.: $this->utils->unparse($text);

TO PARSE use \phpbb\textformatter\s9e\parser - Ie.: $this->parser->parse($text);

--- About how to inject services pls Read The Docs ---

that's another question BTW. This topic is solved as per the file_put_contents()/debug issue.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
mpaw
Registered User
Posts: 141
Joined: Mon Sep 16, 2019 1:09 pm

Re: file_put_contents -> how to save file from extension (debug purpose)

Post by mpaw »

Thank you!
Post Reply

Return to “Extension Writers Discussion”