How to make a file using fopen() fwrite() in slim 3

i have an application where i upload a xml file then it is coverted into json where the json file is being created inside the controller using fopen(), fwrite() php functions. my problem is i keep having a problem on making the json file. i dont know what is the problem, but the same code runs on basic php, it doesnt work in slim 3 framework.

Sample code below:

$file_name = pathinfo($file->getClientFilename(), PATHINFO_FILENAME);

                $filename = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file_name);
                $filename = str_replace(' ', '_', $filename);
                $filename = str_replace('.xml', '', $filename);
                $jsonFileName = $filename . '_' . str_replace(":", "_", $dateTime); //join time and file name to create unique file name
                $jsonFile = __DIR__ . '/../public/' . $jsonFileName . ".json";

//create json file
$jsonWrite = fopen($jsonFile, “w+”);

            if(fwrite($jsonWrite, $json)){
                $success .= 'File parsed from xml to json';
                return $response->withJson($success, 200);

                fclose($jsonWrite);
            } else {
                // failed to write to json file
                $error .= 'failed to write to json file';
                return $response->withJson($error, 200);

            }

the $file_name gets its getClientFilename() from the xml file uploaded. so lets say i uploaded “exam_results.xml” the $file_name will only get exam_results.

so how can i make this code work? or if any other way is possible please help

Have you check the error from apache log?

If no errors, is it success to create new file even blank json file?

If no any new file json, you should debug maybe the path is not correct.

on the access logs it returns code 200 means successfully.

it does not create the json file, and the path i have wrote their is the one i want to direct it too. i changed the path for it to be saved on the same folder as the controller running the function is in but it didnt work. is their a way i can make the json file? also is their a way i can define upload/created files, file path like lets say if folder public/uploads then everything gets stored their automatically.

Code 200 means the response from server to the client is successfuly but not means your function is fine. You should check the error_logs for more acurate information.

I really sure if your path is not correct or maybe it is permission issues from your web server.
Because you cant write a blank file.

If php server doesn’t returns any error, you can force to die like this

// try to open file
$fp = fopen('tester.txt', 'a') or die('fopen failed');
//try to write file
fwrite($fp, "tester to write any text...") or die('fwrite failed');
//close
fclose($fp);

It’s to help determine where is the error happened