Hi guys,
I am currently working on one of my assignments that requires PHP and think of myself as a beginner programmer, only having knowledge of Python and JavaScript. So I have a page that allows me to upload files to the server(no problem here), then I created a drop down menu that shows list of these and depending on which is selected can be viewed after clicking on a submit button(again no problem here). The problem occurs that after clicking submit to save the changes, it refreshes variable containing which file is the target and doesn't save the changes. Tried using sessions, but with no success and I am going absolutely crazy with this. I have been trying to resolve the issue for 5 hours straight. Here is the code if anyone is keen to help me out:
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>P2</title>
<style>
.uploadbox {
background-color: skyblue;
border: 1px, solid black;
margin: 10px;
padding: 5px;
width: 20%;
height: 10%;
line-height: 50px;
}
.selectbox {
background-color: skyblue;
border: 1px, solid black;
margin: 10px;
padding: 5px;
width: 25%;
max-height: 50%;
}
</style>
</head>
<body>
<h1>File Upload</h1>
<form class="uploadbox" action="upload.php" method="post" enctype="multipart/form-data">
Select to upload:
<input type="file" name="fileUpload1" id="fileUpload1">
<input type="submit" value="Upload file" name="submit">
</form>
<br>
<?php
session_start();
$dirpath="uploads/";
$filenames="";
if(is_dir($dirpath))
{
$files=opendir($dirpath);
if($files)
{
while(($filename=readdir($files))!=false)
if($filename!="." && $filename!="..")
{
$filenames=$filenames."<option>$filename</option>";
}
}
}
$url = 'index.php';
$_SESSION['Open'] = htmlEntities($_POST['Open']);
$file="uploads/".$_SESSION['Open'];
// check if form has been submitted
if (isset($_POST['text']))
{
// save the text contents
$newcontent = $_POST['text'];
file_put_contents($file, $newcontent);
// redirect to form again
//header(sprintf('Location: %s', $url));
//printf(htmlspecialchars($url));
// exit();
}
// read the textfile
$text = file_get_contents($file);
echo($newcontent);
echo($file);
echo($_SESSION['Open']);
?>
<div class="selectbox">
<form action="" method="post">
<select name="Open"><?php echo $filenames; ?></select>
<input type="submit" value="Open"/>
</form>
<form action="" method="post">
<textarea style="width:90%; height:20%;" name="text"><?php echo htmlspecialchars($text) ?></textarea>
<br>
<input type="submit" value="Save"/>
</form>
</div>
</body>
</html>