Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda