• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Help with redirect after Upload

chrisj

Coder
The code I'm using on a web page allows the visitor to record a video via web cam and then when Upload is selected the recorded file arrives into the /uploads folder successfully. I'm trying to add a redirect to another page after the successful upload, but I added in the 'header(Location ...' and 'exit' lines, (line 20 & 21) it doesn't reditect. Here is the code:

PHP:
<?php

foreach (array('video', 'audio') as $type) {
    if (isset($_FILES["${type}-blob"])) {
        $fileName = $_POST["${type}-filename"];
       $uploadDirectory = 'uploads/' . $fileName;
            // make sure that one can upload only allowed audio/video files
            $allowed = array(
                'webm',
                'wav',
                'mp4',
                'mov'
    );
    $extension = pathinfo($uploadDirectory, PATHINFO_EXTENSION);
    if (!$extension || empty($extension) || !in_array($extension, $allowed)) {
        echo 'Invalid file extension: '.$extension;
        return;
    }
$new_filepath = "uploads/" . uniqid() . ".". $extension;
header('Location: https://some-website.com');
exit();
if(file_exists($_FILES["${type}-blob"]["tmp_name"]) && is_uploaded_file($_FILES["${type}-blob"]["tmp_name"]))
{
if (!move_uploaded_file($_FILES["${type}-blob"]["tmp_name"], $new_filepath))
{
    echo 'No upload';
    } else {
        echo 'File uploaded';
    }
}
}
}
?>

Any guidance/assistance is appreciated.
 

Ghost

Active Coder
You cannot use your header location after there has been output on the page. It can be a little finicky. If any HTML, other headers, or even blank space has been outputted on the page then it will not redirect. If you have errors enabled on your site and your script has just 1 non-fatal warning (page won't stop, but the page echos out a warning/error) then it will not redirect.

I made something that forces a redirect by trying to do it with PHP, JS, HTML. Most of the time the user is redirected before more can trigger, but it's a "fail proof" way to redirect because if PHP header location fails, it goes to JS and if JS is disabled then it uses HTML. The user gets redirected, and the function exits() the page to make sure nothing after it will load!

Code:
public function forceRedirect($myurlhere){ // used by scripts to redirect to a certain page
    header("Location: $myurlhere"); // redirect to URL
    echo "<script>window.location.href='$myurlhere';</script><meta http-equiv='refresh' content='0; url=$myurlhere' />";
    exit("A redirect to <a href='$myurlhere' target='_blank'>$myurlhere</a> was attempted, but it something has prevented it. This website may not function as intended."); // in case JS is disabled
}

I use this function all over the place on several projects. I have a System class that allows me to just do $System->forceRedirect("https://wubur.com"); or to whatever page. You can also use relative links like $System->forceRedirect("/relative-page.php");
 

Top