find the name of a json file in php

To find the name of a JSON file in PHP, you can use the basename() function along with the __FILE__ constant. Here's an example:

main.php
$jsonFile = "data.json"; // replace with the name of your JSON file
$filename = basename(__FILE__, '.php'); // get the name of the current PHP file without the .php extension
$jsonFilename = $filename . '_' . $jsonFile; // combine the current file name with the JSON file name
echo $jsonFilename; // display the full name of the JSON file
339 chars
5 lines

This code will output the full name of your JSON file, with the current PHP file name as a prefix. You can modify the data.json filename in the first line to match your actual filename. The basename() function strips any directory information and returns just the file name itself. The __FILE__ constant refers to the current PHP file name, so you can use it to dynamically generate the full name of the JSON file. Lastly, the code uses concatenation to join the PHP file name with the JSON file name, separated by an underscore.

gistlibby LogSnag