问题描述
正如标题所说,我希望在用户上传文件后将其重定向回主页,到目前为止,所有这些代码所做的只是显示一个包含相关信息(文件名、文件大小等)的页面.) 我想将它们重定向到自定义成功页面.
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";
}
?>
推荐答案
假设你的成功页面是'success.html',你可以使用:
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.
删除以下几行:
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");
然后使用
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";
}
?>
希望这会有所帮助!
这篇关于如何在使用 PHP 上传文件后重定向用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!