Issue saving text from form to .txt file

Hi, thanks for taking the time.

I have this textarea on my form from 2amigos/TinyMce library that is supposed to take all the text written on it and save it in a newly created .txt file on my server.

It works perfectly offline(Xampp) but has an issue Online where it simply won’t upload any formatted text e.g bold text, underlined, paragraphed e.t.c. If I give it raw_text e.g Yii2 is amazing, then it works perfectly but if I Yii2 is amazing it won’t save, instead gives error (Page not Found).

I think the problem may be my Controller…but what do i know.

Kindly assist even if it means a better way of achieving my goal to save text from TinyMce in a .txt…Thanks in advance

UploadsController





public function actionBooks()

    {

        $this->layout = 'panel';

        $model = new Uploads();


       if ($model->load(Yii::$app->request->post()) && $model->save()) 

       {


            $book_name = $model->book_name;


             $myfile = fopen('../../frontend/web/pdf/uploads/'.$book_name.'.txt', "w+");

             $txt =  $model->book;

             fwrite($myfile, $txt);


             $model->book_path = 'uploads/'.$book_name.'.txt';

             $model->save();

             fclose($myfile);

             

            return $this->redirect(['view', 'id' => $model->book_id]);


            }            

        

            return $this->render('books', [

                'model' => $model,

            ]);

  

    }



Uploads Model Class





    public $book;


    public static function tableName()

    {

        return 'uploads';

    }

    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['book_name', 'book_author'], 'required'],

            [['read_count'], 'integer'],

            [['date_added'], 'safe'],

            [['book_name', 'book_type','subject','read_level', 'book_author','book_path'], 'string', 'max' => 200],

            [['book'],'safe'],

        ];

    }

Form snippet





<?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); ?>


...


<?= $form->field($model, 'book')->widget(TinyMce::className(), [

        'options' => ['rows' => 8],

        'language' => 'en',

        'clientOptions' => [

            'plugins' => [

                "advlist autolink lists link image charmap print preview anchor wordcount pagebreak",

                "searchreplace visualblocks code fullscreen",

                "insertdatetime media table contextmenu paste"

            ],

            'toolbar' => "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist   outdent indent | link image"

        ]

    ])->label('Paste Text Here:');?>


   .....


<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>


 <?php ActiveForm::end(); ?>




A 404? What does the request look like? And what does a working request look like in comparison?

Yes it’s a 404…not sure why but a working request should redirect me to a view page with the id just like the yii2 template does after a record has been inserted. Not sure how to answer your second query about the request…How do i get the request? am sort of still new to this. Thanks

You can use the developer tools of your browser (F12 in Firefox or Chrome) or use Fiddler: http://www.telerik.com/fiddler .

Both requests look similar on my end with fiddler…Only difference is the data I am sending…I think it may have something to do with my controller or maybe fwrite fails…Any ideas?

Also I tried switched from using Tinymce to Ckeditor but results are the same. Can you have a look at my controller please

If you get a 404, it’s more likely that your request is the issue. Please post a working and a failing request.