In PHP I am generating an array with all files I have stored in one repository on the server. I need a list of those files in my javascript.
I saved the PHP array to an .json file.
$json_data = json_encode($posts);
file_put_contents('myfile.json', $json_data);
This file I access via fetch from my javascript.
Now my question. Is this a pracical way to go? I feels like a long stretch to generate an extra .json file simply to get the names of all files stored in one folder. Is there any other way to read the array directly from php?
My PHP File
<?php
$dir = "uploads";
$allCakesPHP = scandir($dir);
$allCakesJSON = json_encode ($allCakesPHP);
header("Location:index.html");
?>
/* file_put_contents("allCakesJSON.json", $allCakesJSON); */
<script type="text/javascript">var jsonData = <?php echo json_encode($allCakesJSON);?> ;</script>
<script type="text/javascript" src="visuTorte.js"></script>
My how i tried to acces the array in javascript
var test = jsonData;