Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

185
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda