0

I am working on my first PHP script which is supposed to upload a file to the server, show the contents of the file and then save any changes to the file.

Here is the code:

<form action="phpfileedit.php" method="post" enctype="multipart/form-data">
               <label for="file">Filename:</label>
               <input type="file" name="file" id="file" />
               <br/>
               <button type="submit" name="submit" value="Submit">Submit button </button>
        </form>
        <?php


            if(isset($_POST['submit'])) {
                if ($_FILES["file"]["error"] > 0) {
                    echo "Error: " . $_FILES["file"]["error"] . " - Please select a file" . "<br />";
                } else {
                    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
                    echo "Type: " . $_FILES["file"]["type"] . "<br />";
                    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
                    echo "Temporarily Stored in: " . $_FILES["file"]["tmp_name"] ." <br />";
                    echo "File Uploaded At: " . date("H:m");
                    echo $name;
                }

                move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
                echo "<br /><br />Your file was moved to: " . $_FILES["file"]["name"];
                #file upload complete 

            echo "<br /><textarea name='textarea'>";
            echo file_get_contents($_FILES["file"]["name"]);
            echo "</textarea>";
            }

            if(isset($_POST['save'])) {
                $file = $_FILES["file"]["name"];
                $fh = fopen($file, 'w');
                $txt = $_POST['textarea'];
                fwrite($fh,$txt);
                fclose($fh);
            }
        ?>
        <form action="phpfileedit.php" method="post" enctype="multipart/form-data">
        <button type="submit" name="save" value="Submit">Save Changes</button>
        </form>

the code that handles the writing to the file (which doesn't work is):

if(isset($_POST['save'])) {
                $file = $_FILES["file"]["name"];
                $fh = fopen($file, 'w');
                $txt = $_POST['textarea'];
                fwrite($fh,$txt);
                fclose($fh);
            }

this should work when the submit button is pressed but all it does is reset the page.

please can you give as much information as you can as I am fairly new to coding.

Thanks in advance for any answers.

Rajbir
  • 411
  • 2
  • 4
  • 13
  • 1
    Enable `error_reporting`. – mario Feb 01 '15 at 20:01
  • @mario do you mean in the php.ini file? if so i do not have access to that as it is a shared server for my college and not my own – Rajbir Feb 01 '15 at 20:10
  • At the top of your php file put ini_set('display_errors', 1); ini_set('display_startup_errors', 1); and error_reporting(E_ALL); Just don't leave it in production. – DevOpsSauce Feb 16 '17 at 06:13

0 Answers0