Reputation: 79
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";
}
?>
Upvotes: 0
Views: 2928
Reputation: 6369
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!
Upvotes: 1