Upload form, then automatically re-direct when complete

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
devster
Forum Newbie
Posts: 2
Joined: Fri Feb 12, 2010 4:05 am

Upload form, then automatically re-direct when complete

Post by devster »

Hi folks,

I was wondering if someone could help me. This is more of a feature than a necessity for my website, but it would be nice.

Basically, I have an upload form where users can upload certain files to the site. We've all seen it. I want it so that after the upload is either successfully uploaded or they get the 'wrong file type' error, I want to either re-direct them back to the home page or back to the uploads page automatically, or within 5 seconds or something.

The uploads section is fairl straighforward. Here is some of the offending code:

The form:

<form action="upload.php" method="post" enctype='multipart/form-data'>
<input type="file" name="myfile"><p>
<input type="submit" value="Upload">

</form>


The php script that checks everything:

<?php

//Properties of uploaded file
$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];

if ($error > 0)
die("Error uploading file!");
else
{
//Conditions for the file type
if($type !== "application/octet-stream")
{
die ("Wrong file type. Please use your browser to navigate back");
}
else if($size > 152000000)
{
die ("File too big. Maximum file size is 80Mb");
}
else
{
move_uploaded_file($temp, "uploads/".$name);
echo "Upload complete! Please use your browser to navigate back";
}
}

?>

Thank you all for your help.

Best regards,

Dev.
dejvos
Forum Contributor
Posts: 122
Joined: Tue Mar 10, 2009 8:40 am

Re: Upload form, then automatically re-direct when complete

Post by dejvos »

Hi,

If you didn't output anything you could use:

Code: Select all

 
header("Location: ".$your_page);
 
If you leave the script as it is you need Javascript. Simply write to the end this:

Code: Select all

 
<script type="text/javascript">
window.location.href="<?php echo $your_page; ?>";
</script>
 
Post Reply