PHP页面刷新不保持变量值

时间:2019-03-10 11:52:05

标签: php

我正在尝试上载正常的文件。按下上载按钮后,我需要刷新页面,以便文件显示在页面上,而用户不必刷新页面。这也很好。我想将易变的$ refresh更改为1,但是即使我在重新加载页面后放置代码,它也会将$ refresh变量保持为2。如何使该变量保持为1。

$refresh = "2";
$dir_path = "Dir10/";
$items = scandir($dir_path);
echo 'List of files in ' . $dir_path;

 foreach($items as $item){
 echo '<li>' . $item . '</li>';
}

echo '<br>';  
echo '<form  method="post"  enctype="multipart/form-data"> 
     <input type="file" name="file1"><br>
     <input type="submit" value="Upload" name="submit">
     </form>';   

if(isset($_POST['submit'])){ //when upload button is press upload file 
  $tmp_name = $_FILES['file1']['tmp_name'];
$path=getcwd() . DIRECTORY_SEPARATOR . $dir_path;
$name = $path . DIRECTORY_SEPARATOR . $_FILES['file1']['name'];
$success = move_uploaded_file($tmp_name, $name);
header("Location:task10.php"); //refresh page
//echo "<meta http-equiv='refresh' content='1'>"; this also does the same 
$refresh = "1";

1 个答案:

答案 0 :(得分:0)

好吧,使用Java脚本可以解决问题。将要刷新的内容放在div标签中,然后在要刷新的位置运行此脚本。

echo '<div id="reload">'   ;
echo '<strong>'.'List of files in Dir10'.'</strong> ';  //display list of files
foreach($items as $item){
 echo '<li>' . $item . '</li>';
}

 echo '<script type="text/javascript">
$("#refresh").load("task10.php #refresh");
</script>';