0

I have had a look around for this answer but I couldn't find one that worked with this script. I just want to redirect to another web page when a user uploads their file and presses submit. The upload works but the redirect doesn't, as currently I've got an echo on there.

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));


// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
      

  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}



?>
  • visit https://stackoverflow.com/questions/15275562/php-redirect-page-after-files-are-uploaded – Nayef Hamad Sep 09 '20 at 21:46
  • You can either echo, or redirect. Not both. It's not logical - if you were to echo and then redirect, the echo would never be seen, because your redirect command is telling the browser to ignore the output from the current script, and go and load another one instead. So you need to choose. If you want to redirect, but still show a custom status message, consider adding it to the session instead. – ADyson Sep 09 '20 at 21:52

1 Answers1

0

You can use the header() function to redirect to another web page just after the echo when the upload is successful.

$URL = "http://example.com";

header('Location: '.$URL);
Yohan
  • 26
  • 2