Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am submitting a form as below

XML
<form action="myfile.php" name ="formSubmit" method="POST" enctype="multipart/form-data">
                <label>Product Code</label>
                <input type="text" name="productCode"><br>
                <label>Product Image</label>
                <input type="file" name="myfile1" />
                <input type="submit" value="Add Product">
                </form>


the 'myfile.php' contains the following line:

XML
<?php
// connect to database
$dbname = mysql_connect("************", "user","password")or die('Cannot select database');
mysql_select_db("mydb")or die('Cannot select database');

// if unable to connect display error
if (!$dbname ) {
echo "Unable to establish connection to database server";
exit;
}

if (!mysql_select_db('mydb', $dbname )) {
echo "Unable to connect to database";
exit;
}

if (($_FILES["myfile1"]["type"] == "image/gif")
|| ($_FILES["myfile1"]["type"] == "image/jpeg") // only allow: .gif & .jpeg
&& ($_FILES["myfile1"]["size"] < 2000000)) // limitation of size: 2MB
{
if ($_FILES["myfile1"]["error"] > 0) // error occurs during uploading
{
echo "Return Code: " . $_FILES["myfile1"]["error"] . "<br />";
} //end if
else
{
// show the basic information of uploaded files
echo "Upload: " . $_FILES["myfile1"]["name"] . "<br />";
echo "Type: " . $_FILES["myfile1"]["type"] . "<br />";
echo "Size: " . ($_FILES["myfile1"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["myfile1"]["tmp_name"] . "<br />";

// check whether the file exists in the target folder
if (file_exists("upload/" . $_FILES["myfile1"]["name"])){
echo $_FILES["myfile1"]["name"] . " already exists. ";
} //end if

// do the uploading: moving the file from temporary folder to the target folder
else
{
move_uploaded_file($_FILES["myfile1"]["tmp_name"],
"upload/" . $_FILES["myfile1"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["myfile1"]["name"] . "<br />";

//store filename in the database together with the product code
$name = $_FILES["myfile1"]["name"];
$result = mysql_query("insert into images values ('$productCode','$name')");
echo "Product " . $productCode . " successfully added to the database." . "<br />" ;
} //end else
} //end else
} // end if

else
{
echo "Invalid file";
}
echo "finish";
header("Location: admin.php");
//header("Location: ".htmlentities($_SERVER['HTTP_REFERER']));
//header("Location: ".$_POST["formSubmit"]);

?>




My problem that once all the contents in 'myfile.php' are successfully executed the header command is not working. Any ideas please?
Posted
Updated 27-Dec-13 23:24pm
v4
Comments
Thomas Daniels 28-Dec-13 4:42am    
What do you mean by "not working"? Do you get errors? Do you get redirected to the wrong page? Do you don't get redirected at all?
datt265 28-Dec-13 5:12am    
I do not get re-directed at all
datt265 28-Dec-13 5:13am    
no errors are issued. the loaded page remains 'myfile.php'

1 solution

There is nothing in the $_POST["formSubmit"]. Assuming the first page is called "sendfrom.php", then change header in the "myfile.php" as follows:
1. The static way:
header("Location: sendfrom.php");

Or,
2. The dynamic way:
header("Location: ".htmlentities($_SERVER['HTTP_REFERER']));


Looking at the added code, another problem surfaces => echo cannot come before header. Check this out.
example
 
Share this answer
 
v5
Comments
datt265 28-Dec-13 5:25am    
No still cannot tell why it is not working, I have updated the question and put all the contents of 'myfile.php'
Peter Leow 28-Dec-13 6:02am    
I can see the problem clearer now. You can not have echo before header. Check this out.
example

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900