使用php上传文件后如何重定向到成功页面?

发布于 2025-01-11 01:56:48 字数 1848 浏览 0 评论 0原文

我试图在成功上传后重定向到另一个页面。

所以我在 stackoverflow 上搜索了类似的答案,但似乎没有解决我的问题。

这是我的表单:

    <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>

这是我的 php,其中包含重定向的标头

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你爱我像她 2025-01-18 01:56:48

请记住,在发送任何实际输出之前,必须调用 header(),无论是通过普通 HTML 标记、文件中的空行还是从 PHP 发送。

PHP 标头(来自 php.net)

WITH PHP:

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

如果你愿意要使用输出,首先使用 HtmlJavascript 代码

WITH HTML:

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

使用 JavaScript:

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

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文