douyuanliao8815 2014-01-26 19:06
浏览 24
已采纳

如何在使用PHP上传文件后重定向用户

As the title says, I wish to redirect a user back to the main page after they have uploaded a file, so far, all this code does is display a page with the relevant information (File name, file size etc..) I want to redirect them to a custom success page.

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";
 }
 ?>
  • 写回答

1条回答 默认 最新

  • duandiao3961 2014-01-26 19:17
    关注

    Assuming that your success page is 'success.html', you can use:

        header('Location: success.html');
    

    You need to remove ALL your echo lines, since it will cause you an 'headers already sent' exception.

    From a workflow persepective, there is also no sense in echoing to the screen and then redirecting.

    Remove the following lines:

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

    Also you can store all file types in array and make your code much cleaner:

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

    and then use

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

    Full code:

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

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输