Home > Net >  How to redirect to success page after uploading file using php?
How to redirect to success page after uploading file using php?

Time:03-02

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/>";
        }
    }
}
?>

CodePudding user response:

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.

(From php.net) php header

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

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

  • Related