2016-11-02 2 views
0

Bearbeitung: als Duplikat markiert, aber die Lösungen in der Post haben in meinem Fall nicht geholfen, als ich es versuchte. Ich will es jetzt nicht mit Ajax machen, also habe ich nur window.location zum Redirect benutzt. Ich verliere die Namen von hochgeladenen Dateien, aber ich würde eher damit umgehen, diese irgendwie zu passieren.Dateien, die nach der Aktualisierung der Seite gepostet werden

Ich habe ein Formular zum Hochladen von Dateien, ich posten auf der gleichen Seite, nach dem Absenden der Datei hochgeladen wird, aber wenn ich die Dateien aktualisieren werden neu geladen. Ich habe versucht, $ _POST und $ _FILES auf ein leeres Array am Ende des Upload-Skripts zu setzen, aber immer noch die Post-Daten jedes Mal .. Auch versucht, einen Header hinzuzufügen, aber es heißt, sie sind bereits gesendet, wenn ich es versuche um am Anfang des Skripts ob_start zu verwenden, keine Änderung.

mein Tisch und Form sieht aus wie diese

<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>' 

Und hier ist der Upload-Skript:

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>"; 
} 

}

+1

Wenn Sie f5 drücken, wird die Seite über senden die POST-Anfrage erneut. Sie sollten Ajax verwenden. Lesen Sie über Ajax [auf MDN] (https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started) – natanelg97

+0

Ja, ich dachte, es wäre eine einfache Möglichkeit, nur die Daten zu löschen, aber ich denke, das ist jedenfalls besser. – nick

Antwort

1

Request Header Ihre Daten gespeichert werden. Wenn Sie also aktualisieren, werden Daten erneut gesendet.

Sie haben 3 Lösungen:

  1. teilen Sie Ihren Code in 2 verschiedene Seite

  2. Verwendung Ajax (ofcourse dieser Notwendigkeit, die Seite wie kein Split 1)

  3. try zu umleiten in eine andere Seite und dann erneut auf Ihre Formularseite umleiten.

3. Art und Weise zu verwenden, können Sie versuchen, diese

index.php

<html> 
<head><title>asdas</title></head> 
<body><!--i give u 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 u 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> 
Verwandte Themen