0

I'm trying to use the following script to redirect but it executes and then hangs before it redirects. Just before the html I have the header() command.

 <?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> 
DCR
  • 14,737
  • 12
  • 52
  • 115
  • 1
    Are you getting form data? If not then that is the issue, because redirect is placed inside IF block and executes if condition is true only. – Masood Oct 28 '19 at 20:32
  • I get form data and the file is uploaded – DCR Oct 28 '19 at 20:40
  • Possible duplicate of [Php header location redirect not working](https://stackoverflow.com/questions/21226166/php-header-location-redirect-not-working) – Masood Oct 28 '19 at 20:47
  • Here it has been already answered. https://stackoverflow.com/questions/21226166/php-header-location-redirect-not-working/30283281 – Masood Oct 28 '19 at 20:47
  • These DON'T answer the question as I am not outputting anything before the redirect – DCR Oct 28 '19 at 21:36
  • every time I visit your site and use this script I get the javascript alert or the redirect ... what isn't working? – lagbox Oct 28 '19 at 23:21

1 Answers1

1

There is a whitespace before your opening <?php tag. This is causing the header redirection to fail, as output (the whitespace) has been sent to the browser before the header redirection.

rubenccdev
  • 161
  • 4