Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ankur_g's avatar

Facing issue to convert a json file into csv

The json is as follows:

{
  "personality_test_id": "27",
  "qna": [
    {
      "questionNo": 1,
      "question": "I am the life of the party.",
      "answer": 2
    },
    {
      "questionNo": 2,
      "question": "I feel little concern for others.",
      "answer": 4
    },
    {
      "questionNo": 3,
      "question": "I am always prepared.",
      "answer": 2
    },
    {
      "questionNo": 4,
      "question": "I get stressed out easily.",
      "answer": 4
    },
    {
      "questionNo": 5,
      "question": "I have a rich vocabulary.",
      "answer": 2
    },
    {
      "questionNo": 6,
      "question": "I don't talk a lot.",
      "answer": 3
    },
    {
      "questionNo": 7,
      "question": "I am interested in people.",
      "answer": 3
    },
    {
      "questionNo": 8,
      "question": "I leave my belongings around.",
      "answer": 2
    },
    {
      "questionNo": 9,
      "question": "I am relaxed most of the time.",
      "answer": 3
    },
    {
      "questionNo": 10,
      "question": "I have difficulty understanding abstract ideas.",
      "answer": 3
    },
    {
      "questionNo": 11,
      "question": "I feel comfortable around people.",
      "answer": 3
    },
    {
      "questionNo": 12,
      "question": "I insult people.",
      "answer": 2
    },
    {
      "questionNo": 13,
      "question": "I pay attention to details.",
      "answer": 4
    },
    {
      "questionNo": 14,
      "question": "I worry about things.",
      "answer": 4
    },
    {
      "questionNo": 15,
      "question": "I have a vivid imagination.",
      "answer": 3
    },
    {
      "questionNo": 16,
      "question": "I keep in the background.",
      "answer": 3
    },
    {
      "questionNo": 17,
      "question": "I sympathize with others' feelings.",
      "answer": 4
    },
    {
      "questionNo": 18,
      "question": "I make a mess of things.",
      "answer": 2
    },
    {
      "questionNo": 19,
      "question": "I seldom feel blue.",
      "answer": 4
    },
    {
      "questionNo": 20,
      "question": "I am not interested in abstract ideas.",
      "answer": 2
    },
    {
      "questionNo": 21,
      "question": "I start conversations.",
      "answer": 3
    },
    {
      "questionNo": 22,
      "question": "I am not interested in other people's problems. \t",
      "answer": 2
    },
    {
      "questionNo": 23,
      "question": "I get chores done right away.",
      "answer": 3
    },
    {
      "questionNo": 24,
      "question": "I am easily disturbed.",
      "answer": 3
    },
    {
      "questionNo": 25,
      "question": "I have excellent ideas.",
      "answer": 3
    },
    {
      "questionNo": 26,
      "question": "I have little to say.",
      "answer": 2
    },
    {
      "questionNo": 27,
      "question": "I have a soft heart.",
      "answer": 4
    },
    {
      "questionNo": 28,
      "question": "I often forget to put things back in their proper place.",
      "answer": 2
    },
    {
      "questionNo": 29,
      "question": "I get upset easily.",
      "answer": 3
    },
    {
      "questionNo": 30,
      "question": "I do not have a good imagination.",
      "answer": 2
    },
    {
      "questionNo": 31,
      "question": "I talk to a lot of different people at parties.",
      "answer": 3
    },
    {
      "questionNo": 32,
      "question": "I like order.",
      "answer": 3
    },
    {
      "questionNo": 33,
      "question": "I change my mood a lot.",
      "answer": 4
    },
    {
      "questionNo": 34,
      "question": "I am quick to understand things.",
      "answer": 3
    },
    {
      "questionNo": 35,
      "question": "I don't like to draw attention to myself.",
      "answer": 4
    },
    {
      "questionNo": 36,
      "question": "I take time out for others.",
      "answer": 4
    },
    {
      "questionNo": 37,
      "question": "I shirk my duties.",
      "answer": 2
    },
    {
      "questionNo": 38,
      "question": "I have frequent mood swings.",
      "answer": 3
    },
    {
      "questionNo": 39,
      "question": "I use difficult words.",
      "answer": 2
    },
    {
      "questionNo": 40,
      "question": "I don't mind being the center of attention.",
      "answer": 2
    },
    {
      "questionNo": 41,
      "question": "I feel others' emotions.",
      "answer": 4
    },
    {
      "questionNo": 42,
      "question": "I follow a schedule.",
      "answer": 4
    },
    {
      "questionNo": 43,
      "question": "I get irritated easily.",
      "answer": 3
    },
    {
      "questionNo": 44,
      "question": "I spend time reflecting on things.",
      "answer": 3
    },
    {
      "questionNo": 45,
      "question": "I am quiet around strangers.",
      "answer": 4
    },
    {
      "questionNo": 46,
      "question": "I make people feel at ease.",
      "answer": 3
    },
    {
      "questionNo": 47,
      "question": "I am exacting in my work.",
      "answer": 3
    },
    {
      "questionNo": 48,
      "question": "I often feel blue.",
      "answer": 4
    },
    {
      "questionNo": 49,
      "question": "I am full of ideas.",
      "answer": 3
    },
    {
      "questionNo": 50,
      "question": "I am full of ideas.",
      "answer": 3
    }
  ]
}

The controller code is as follows:

$csv = public_path().'/iq_data.csv';
            $fp = fopen( $csv , 'w' );
            foreach( $request->qna as $questions ) {
                $personaqnaobject = new PersonaPersonalityTestsQna();
                $personaqnaobject->personality_test_id = $request->personality_test_id;
                $personaqnaobject->question            = $questions['question'];
                $personaqnaobject->answer              = $questions['answer'];
                $personaqnaobject->submit_count        = $count;
                $personaqnaobject->request_type        = strtolower( $PersonaPersonalityTest->request_type );
                $personaqnaobject->save();
                fputcsv($fp, $questions['question']);
                fputcsv($fp, $questions['answer']);
                if( !empty( $answer_array['text'] ) ) {
                    $answer_array['text'] .= ', '. $questions['answer'];
                }else {
                    $answer_array['text'] = $questions['answer'];
                }
                  
                $personascoreobject = new PersonaPersonalityScore();
                $personascoreobject->personality_test_id = $request->personality_test_id;
                $personascoreobject->submit_count        = $count;

            }
            fclose( $fp );

Whenever I am hitting the URL it gives an error as follows:

ErrorException: fopen(/opt/lampp/htdocs/multitenant/public/iq_data.csv): failed to open stream: Permission denied in file /opt/lampp/htdocs/multitenant/app/Http/Controllers/PersonaController.php on line 408

The permission to the public folder is denied and also the .csv file is not being created

0 likes
6 replies
Sinnbeck's avatar

Seems that you are not allowed to open the file? Maybe it is locked by another process (open in another program) ?

Snapey's avatar

write the file somewhere else? By default the public folder SHOULD be read-only

tykus's avatar
tykus
Best Answer
Level 104

Does the web server system user (www-data) have permissions necessary to open and write the file?

ankur_g's avatar

@tykus I think the permission needs to be changed; although the public folder has already permission to write onto it

Please or to participate in this conversation.