Jump to content

Please help, I am going crazy


Dralexus

Recommended Posts

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>

 

Link to comment
Share on other sites

14 minutes ago, requinix said:

What do you mean by "save the changes"? What is supposed to be saved? Where?

I am using a form with a text area to change the contents of a text file. The file itself is chosen from a dropdown menu that is also used to open the text file of choice. When I try to use file_put_contents to overwrite with the submit method, the variable containing the name of the file chosen becomes null/nothing/get removed, so file_put_contents cant overwrite the file due to the directory being incorrect.

Link to comment
Share on other sites

You have two forms, one to let you open the file and another to let you save contents to the file. The first form has the filename, the second form has the file contents.

The question for you is: why would the second form also have the filename? You didn't put a filename in there. It's just the textarea. Your browser isn't going to take the filename from the first form and include it in the second form's data.

So put both into one form. Filename and file contents. Put the two buttons into the form too. Since it's just the one form now, you'll need a different method to determine what your code should do. Solve that by giving a name to your button, and you might as well use the same name for both since the user can only click one of them, then checking (a) if $_POST has that button and (b) what value it has.

Side note: <button> buttons are better than <input> buttons.

<button type="submit" name="action" value="open">Open</button>

 

But before you do any of this, open up your php.ini (should be where you installed PHP), find the error_reporting and display_errors settings (the real settings, not the ones that are part of the documentation inside the file), and set them to

error_reporting = -1
display_errors = on

Then restart your web server and try running your code as it is right now. You should see a number of error messages. Do the changes I said above and run it all, then if you still see errors you need to go in and start fixing them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.