php - Customizable Path for file_get_contents based on Session
one text
I dont know how to solve this issue, I have tried to look for an answer, but all I get is that I should put it between .'filename'.
like this file_get_contents('../jsonFIle/' . $_SESSION['nameF'] . '.json');
I am trying to let the user input an existing json file name and it should change the context of the website from that json file. I am also a beginner and any links to how I could solve this would be appreciated
Index.php
<?php
session_start();
$local_dir = 'C:\xampp\htdocs\jsonFile';
$files = checkDir($local_dir);
$_SESSION['nameF'] = $_POST['fname'];
function checkDir($dir)
{
return array_values(array_diff(scandir($dir) , array(
'.',
'..'
)));
}
//default json file
if (count($files) == 0 || session_id() !== '')
{
$jsonData = file_get_contents('../jsonFIle/Ass2News.json');
$json = json_decode($jsonData, true);
}
//new updated file
else
{
$jsonData = file_get_contents('../jsonFIle/' . $_SESSION['nameF'] . '.json');
$json = json_decode($jsonData, true);
}
?>
input.php
<?php
session_start();
include ("connection.php");
include ("functions.php");
$user_data = check_login($con);
if (isset($_POST['submit'])){
$fname=$_POST['fname'];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Update News File</title>
</head>
<body>
<form action='../test/index.php' method='post'>
<label for="fname">File name:</label>
<input type="text" name="fname"><br><br>
<input type="submit" id="buttonS"name="apply" value="Apply">
</form>
</body>
</html>
Source