【问题标题】:How to redirect user after file upload using PHP如何在使用 PHP 上传文件后重定向用户
【发布时间】:2014-01-27 03:47:36
【问题描述】:

正如标题所说,我希望在用户上传文件后将其重定向回主页面,到目前为止,这些代码所做的只是显示一个包含相关信息(文件名、文件大小等)的页面。 ) 我想将它们重定向到自定义成功页面。

HTML:

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

PHP:

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
 "Upload: " . $_FILES["file"]["name"] . "<br>";
 "Type: " . $_FILES["file"]["type"] . "<br>";
 "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  header('Location: success.html');
  }
  }
 }
 else
 {
   echo "Invalid file";
 }
 ?>

【问题讨论】:

    标签: php redirect upload


    【解决方案1】:

    假设你的成功页面是'success.html',你可以使用:

        header('Location: success.html');
    

    您需要删除所有回显行,因为这会导致“标头已发送”异常。

    从工作流程的角度来看,回显到屏幕然后重定向也是没有意义的。

    删除以下行:

    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    

    您还可以将所有文件类型存储在数组中,让您的代码更加简洁:

    $allowedTypes = array("image/gif",..,..,"image/png");
    

    然后使用

    in_array($_FILES["file"]["type"], $allowedTypes);
    

    完整代码:

    <?
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $allowedTypes = array("image/gif",'..','..',"image/png");
    
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);
    if ((in_array($_FILES["file"]["type"], $allowedTypes))
       && ($_FILES["file"]["size"] < 20000)
       && in_array($extension, $allowedExts))
    {
    if ($_FILES["file"]["error"] > 0)
    {
       echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
    
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
      else
      {
        move_uploaded_file($_FILES["file"]["tmp_name"],  "upload/" . $_FILES["file"]["name"]);
        header('Location: success.html');
      }
      }
    }
    else
    {
    echo "Invalid file";
    }
    ?>
    

    希望这会有所帮助!

    【讨论】:

    • 我现在收到此错误;警告:无法修改标头信息 - 标头已由 /srv/disk12/1369030/www/dayz- 中的 /srv/disk12/1369030/www/dayz-survivorguide.co.nf/upload_file.php:20 发送第 33 行的survivorguide.co.nf/upload_file.php
    • 你必须更换回声线。在屏幕上进行任何输出后,您无法设置标题。
    • 我已经替换了回声线,我现在更新我的代码来帮助你更多
    • 您还需要删除其他 4 个回声。如果您要重定向,无论如何保留它们是没有意义的。它们会导致错误。阅读我修改后的答案。
    • 好的,很抱歉让您感到痛苦,非常感谢您的帮助,但它仍然没有重定向我,它只是显示了upload_file.php的空白页面,我再次更新了我的代码
    猜你喜欢
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 2019-07-18
    • 2010-10-16
    • 1970-01-01
    • 2012-01-30
    • 2013-03-02
    相关资源
    最近更新 更多