0

edit: marked as duplicate, but the solutions in post didn't help in my case when I tried. Don't want to do it with ajax right now, so I just used window.location to redirect. I lose the names of uploaded files, but I'd rather deal with passing those through somehow.

I have a form to upload files, I post to the same page, after submit the file is uploaded but if I refresh the files are reuploaded. I've tried to set $_POST and $_FILES to a blank array at the end of the upload script, but still keeps the post data every time .. Also tried adding a header, but it says they are already sent, when I try to use ob_start at the beginning of script, no change.

my table and form looks like this

<table name="doctor_file_table" width="100%">
            <tr>
                <td><b>Name</b></td>
                <td><b>Type</b></td>
                <td><b>Size (bytes)</b></td>
                <td><b>Created</b></td>
               <td><a href='path_to_file' download =''>Download</a></td>
                <td><button id ='id#' onClick='deleteFile(this)' type='button'>Delete</button></td>
            </tr>

<form action="" enctype="multipart/form-data" method="post">
    <div>
        <label for="upload">Add Attachments:</label>
        <input id="upload" name="upload[]" type="file" multiple="multiple"/>
    </div>
        <p><input type="submit" name="submit" value="Submit"></p>
    </form>'

And here is the upload script:

if(isset($_POST['submit']) && $_POST['uploaded'] == 1){
echo $_POST['uploaded'];
if(count($_FILES['upload']['name']) > 0){
    //Loop through each file
    for($i=0; $i<count($_FILES['upload']['name']); $i++) {
      //Get the temp file path
        $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

        //Make sure we have a filepath
        if($tmpFilePath != ""){

            //save the filename
            $shortname = $_FILES['upload']['name'][$i];
            //save the url and the file
            $filePath = "/var/www/html/doctor_files/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];
            $fullname = substr($filePath,27);
            //Upload the file into the temp dir
            if(move_uploaded_file($tmpFilePath, $filePath)) {

                $files[] = $shortname;
            $sql = 'insert into '.TABLE_DOCTOR_FILES.'(shortname,fullname,filepath,type,size,doctor_id) VALUES("'.$shortname.'", "'.$fullname.'", "'.$filePath.'", "'.$_FILES["upload"]["type"][$i].'",'.$_FILES["upload"]["size"][$i].',"'.$doctor_id.'")';                    
            database_void_query($sql);

             //use $shortname for the filename
                //use $filePath for the relative url to the file

            }
          }
    }
}

//show success message
echo "<h1>Uploaded:</h1>";    

if(is_array($files)){
    echo "<ul>";
    foreach($files as $file){
        echo "<li>$file</li>";
    }
    echo "</ul>";
}

}

nick
  • 789
  • 1
  • 11
  • 27
  • 1
    When you press f5, the page is about to send the POST request again. You should use ajax. read about ajax [on MDN](https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started) –  Nov 02 '16 at 19:22
  • Yea, I thought there'd be an easy way to just clear the data, but I guess that's better anyway. – nick Nov 02 '16 at 19:25

1 Answers1

1

Request Header will store your data. So if you refresh, data will sent back again.

You have 3 solutions:

  1. split your code into 2 different page

  2. use ajax (of course this needs to split the page like no 1)

  3. try to redirect into another page, then redirect again to your form page.

To use the third way, you can try this:

index.php

<html>
<head><title>asdas</title></head>
<body><!--i give you header to know this is will give error header or not-->
<?php
if(isset($_POST['submit']) && $_POST['uploaded'] == 1){
echo $_POST['uploaded'];
if(count($_FILES['upload']['name']) > 0){
    //Loop through each file
    for($i=0; $i<count($_FILES['upload']['name']); $i++) {
      //Get the temp file path
        $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

        //Make sure we have a filepath
        if($tmpFilePath != ""){

            //save the filename
            $shortname = $_FILES['upload']['name'][$i];
            //save the url and the file
            $filePath = "/var/www/html/doctor_files/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];
            $fullname = substr($filePath,27);
            //Upload the file into the temp dir
            if(move_uploaded_file($tmpFilePath, $filePath)) {

                $files[] = $shortname;
            $sql = 'insert into '.TABLE_DOCTOR_FILES.'(shortname,fullname,filepath,type,size,doctor_id) VALUES("'.$shortname.'", "'.$fullname.'", "'.$filePath.'", "'.$_FILES["upload"]["type"][$i].'",'.$_FILES["upload"]["size"][$i].',"'.$doctor_id.'")';                    
            database_void_query($sql);

             //use $shortname for the filename
                //use $filePath for the relative url to the file

            }
          }
    }
}

//show success message
echo "<h1>Uploaded:</h1>";    
header('Location: http://localhost/stackoverflow/success.php');
if(is_array($files)){
    echo "<ul>";
    foreach($files as $file){
        echo "<li>$file</li>";
    }
    echo "</ul>";
}

?>


<table name="doctor_file_table" width="100%">
            <tr>
                <td><b>Name</b></td>
                <td><b>Type</b></td>
                <td><b>Size (bytes)</b></td>
                <td><b>Created</b></td>
               <td><a href='path_to_file' download =''>Download</a></td>
                <td><button id ='id#' onClick='deleteFile(this)' type='button'>Delete</button></td>
            </tr>

<form action="" enctype="multipart/form-data" method="post">
    <div>
        <label for="upload">Add Attachments:</label>
        <input id="upload" name="upload[]" type="file" multiple="multiple"/>
    </div>
        <p><input type="submit" name="submit" value="Submit"></p>
    </form>

success.php

<html><head><title>redirect</title></head>
<body><!--i give you header to know this is will give error header or not-->
<?php
echo "<p>Your upload is success</p>";   
echo "<p>You will redirect back</p>";
echo "<p><a href=\"index.php\">or press this to redirect directly</a></p>";
?>
<script>
setTimeout(function () {
       window.location.href = "index.php"; 
    }, 3000);

</script>
</body></html>
halfer
  • 19,824
  • 17
  • 99
  • 186
plonknimbuzz
  • 2,594
  • 2
  • 19
  • 31