Page 1 of 1

Upload form, then automatically re-direct when complete

Posted: Fri Feb 12, 2010 4:12 am
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.

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

Posted: Fri Feb 12, 2010 5:15 am
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>