Abdulhakeem Imran
Abdulhakeem Imran

Reputation: 138

How to redirect to success page after uploading file using php?

I am trying to redirect to another page after a successful upload.

So I searched for similar answers on stackoverflow but non seems to solve my problem.

This is my form:

    <form enctype="multipart/form-data"
        action="<?php print $_SERVER['PHP_SELF']?>"  method="post">
    <p><input type="hidden" name="MAX_FILE_SIZE" value="9000000" /> <input
        type="file" name="pdfFile" /><br />
    <br />
    <input type="submit" value="upload"  /></p>
    </form>

This is my php which includes the header that redirects

<?php
if ( isset( $_FILES['pdfFile'] ) ) {
    if ($_FILES['pdfFile']['type'] == "application/pdf") {
        $source_file = $_FILES['pdfFile']['tmp_name'];
        $dest_file = "upload/".$_FILES['pdfFile']['name'];

        if (file_exists($dest_file)) {
            print "The file name already exists!!";
        }
        else {
            move_uploaded_file( $source_file, $dest_file )
            or die ("Error!!");
            if($_FILES['pdfFile']['error'] == 0) {
                print "Pdf file uploaded successfully!";
                print "<b><u>Details : </u></b><br/>";
                print "File Name : ".$_FILES['pdfFile']['name']."<br.>"."<br/>";
                print "File Size : ".$_FILES['pdfFile']['size']." bytes"."<br/>";
                print "File location : upload/".$_FILES['pdfFile']['name']."<br/>";
                header('Location: success.php');
            }
        }
    }
    else {
        if ( $_FILES['pdfFile']['type'] != "application/pdf") {
            print "Error occured while uploading file : ".$_FILES['pdfFile']['name']."<br/>";
            print "Invalid  file extension, should be pdf !!"."<br/>";
            print "Error Code : ".$_FILES['pdfFile']['error']."<br/>";
        }
    }
}
?>

Upvotes: 0

Views: 246

Answers (1)

Z0OM
Z0OM

Reputation: 960

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

PHP HEADER (From php.net)

WITH PHP:

<?php header("Location: LOADFILE.php"); ?>

If you want to use outputs first use Html or Javascript code

WITH HTML:

 <meta http-equiv="refresh" content="2;URL=http://stackoverflow.com">

WITH JAVASCRIPT:

 <script>window.location.replace("http://stackoverflow.com");</script>

Upvotes: 0

Related Questions