上传文件后的PHP重定向页面

时间:2013-03-07 15:48:41

标签: php redirect upload

我希望在上传文件后重定向回我的主页,我当前有一个echo语句,表明文件已经上传(完成后)。任何帮助将不胜感激!

<?php
  $tags[]= $_POST['Sport'];
  $tags[]= $_POST['bcTags1'];
  $tags[]= $_POST['bcTags2'];
  $tags[]= $_POST['bcTags3'];
  $tags[]= $_POST['hiddenTag'];
  $metaData = array(
    'name' => $_POST['bcVideoName'],
    'shortDescription' => $_POST['bcShortDescription'],
    'tags' => $tags
  );
# Rename the video file     
       $file = $_FILES['theVideoFile'];     
       rename($file['tmp_name'], '/tmp/' . $file['name']);     
       $file_location = '/tmp/' . $file['name'];      
       # Send video to Brightcove     
       $id = $bc->createMedia('video', $file_location, $metaData);

echo $id.'Uploaded Successfully!';
?>

5 个答案:

答案 0 :(得分:2)

通常只使用header()

  //echo $id.'Uploaded Successfully!'; <--- output breaks header()
  header('Location: /yourpage.php');
  exit;
?>

答案 1 :(得分:2)

header("Location: yourscript.php");
die();

这将重定向,但必须在ANYTHING回显之前使用。

答案 2 :(得分:1)

请注意,在输出写入后(例如echo 'uploaded';),您无法修改标题。此外,您可能无法始终重定向(具体取决于脚本中的内容),除非您在使用exit;修改标头后立即结束脚本。

header('Location: http://example.com'); exit;

答案 3 :(得分:1)

如果还没有输出,你可以代替echo语句:

header('Location: http://www.google.com');
die;

答案 4 :(得分:1)

如果上传正常,您可以将变量(例如:$ success)设置为true,然后在页面下方添加以下内容;

<?php

if($success == true){
  echo "<script language='javascript'>\n";
  echo "alert('Upload successful!'); window.location.href='http://splash21.com';";
  echo "</script>\n";
}

?>

这将允许您处理成功和失败的上传