Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
Read php array from javascript using the Fetch.API

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;
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!