【问题标题】:php does not redirect to new page when no other output is being sent to browser当没有其他输出发送到浏览器时,php 不会重定向到新页面
【发布时间】:2020-02-24 03:12:07
【问题描述】:

我正在尝试使用以下脚本进行重定向,但它会在重定向之前执行然后挂起。就在 html 之前,我有 header() 命令。

 <?php



require("../../../../wp-load.php");



if(isset($_POST['submit'])){

   $file            = $_FILES['file'];
   $fileName        = $file['name'];
   $fileType        = $file['type'];
   $fileTmpLocation = $file['tmp_name'];
   $fileSize        = $file['size'];
   $fileError       = $file['error'];


   if(($_POST['link']==='') && $_FILES['file']['name']===''){
       unset($_POST);
       unset($_FILES); 
       echo '<script language="javascript">';
       echo 'alert("You must specify either a link or a file to upload")';
       echo '</script>';
   }

   if ($_FILES['file']['name']!='' && $fileError !== 0){
       unset($_POST);
       unset($_FILES); 
       echo '<script language="javascript">';
       echo 'alert("There was an error uploading your file, try again")';
       echo '</script>';
   }

   if ($fileSize >500000){
       unset($_POST);
       unset($_FILES); 
       echo '<script language="javascript">';
       echo 'alert("Your file is too large. Please limit file size to 5MB")';
       echo '</script>';
   }

   $name = explode('.',$fileName);

   if($_FILES['file']['name']!='' && count($name)!= 2){
       echo '<script language="javascript">';
       echo 'alert("File names must be in form fileName.fileType only 1 . allowed")';
       echo '</script>';
   }

   $fileNameSave = uniqid($name[0].'_',true).'.'.$name[1];
   $pos = strpos($fileNameSave, '.');
   if ($pos !== false) {
     $fileNameSave = substr_replace($fileNameSave, '', $pos, strlen('.'));
   }


   move_uploaded_file($fileTmpLocation,"../uploads/".$fileNameSave);
   unset($_POST);
   header("Location: https://www.sustainablewestonma.org/update.php");
   exit;
}



?>
<!DOCTYPE html>
<html>
   <head>
      <title>Update ITN</title>
      <style>#d1{text-align:center;}#d3{width:30vw;margin:0 auto;}</style>
   </head>
   <body>
     <div id='d1'>
        <h1>SustainableWestonMA.org</h1>
        <h2>In-The-News</h2>
     </div>

     <div id='d3'>

       <form action="update_itn.php" method="POST" enctype="multipart/form-data">
         <pre>
          Date:    <input type="date" name="date"  id="date" class="date" /><br>
          Event:   <input type="text" name="event" id="event"  /><br>
          Link:    <input type="text" name="link"  id="link"/><br>
          or File: <input type="file" name="file"  id="file"/><br>
          <button type="submit" name="submit" id="submit">Update</button></pre>
       </form>
     </div>
      <div id='d2'>
        <pre>In the example below:

     October 7, 2019 Survey finds 292 gas leaks in Weston 
     https://weston.wickedlocal.com/news/20191007/survey-finds-292-gas-leaks-in-weston

     date  = October 7, 2019 
     Event = Survey finds 292 gas leaks in Weston 
     Link  = https://weston.wickedlocal.com/news/20191007/survey-finds-292-gas-leaks-in-weston

     You must specify a link or a file you want to link to but NOT both. File uploads are limited to 5MB</pre></div>
   </body>
</html> 

【问题讨论】:

标签: php redirect header


【解决方案1】:

在您的开始 &lt;?php 标记之前有一个空格。这导致标头重定向失败,因为在标头重定向之前已将输出(空格)发送到浏览器。

【讨论】:

    猜你喜欢
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 2015-02-23
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    相关资源
    最近更新 更多