-3
<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
    $error = "Error";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 2024) . " kB<br>";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?> 

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$new =  "upload/" . $_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"] < 1024)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        $error = "File Error";
    }
  else
    {
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
            $error = "File already exists";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);

      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
        $error = "Uploaded";          
      }
    }
  }
else
  {
  echo "Invalid file";

?> 
<?php
$cid = htmlentities($_GET['id']);
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = "INSERT INTO photo (cid, path) VALUES ('$cid', '$new')";


mysql_select_db('churchinfo');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "<br> Entered data successfully\n";
mysql_close($conn);

?>
</body>
<?php
    $url = "?id=" . $cid  . "&res=" . $error;
    header('Location: photo.php'.$url);

?>
<script>
 location.href = "photo.php?" + <?php echo $error; ?> ; 
</script>
<br />

</body>
</html>

Dear Friends,
This code is working in the localhost. Files also uploading to the folder.
but When I trying in live server

Headers already sent

error message is appearing. And file is also not at the /upload folder.
I read some articles in stackoverflow. But I have no idea to fix this error.

What should I do?
Please help me.

Jenz
  • 8,280
  • 7
  • 44
  • 77

4 Answers4

3

Headers have to be sent first, before any content (HTML or white space).

Look at your code, the header comes after the closing body tag

</body>

<?php
    $url = "?id=" . $cid  . "&res=" . $error;
    header('Location: photo.php'.$url);

?>

Put it at the top, before any output.

David Houde
  • 4,835
  • 1
  • 20
  • 29
1

You'll probably see this error because something has been sent to the client already, such as HTML. There's various ways around this; - I'll demonstrate one way.

ob_start(); <-- put this at the very top of your "brain" file
echo "Welcome to my page";
//Headers already sent error
ob_clean(); <-- clean the output buffer, giving you the ability to send headers
header(....

If you look at the above code

If you run the code without ob_*, you'll get the error you're currently experiencing. However, cleaning the output buffer just before headers are sent, you won't get this error.

If this isn't an option for you, rearrange your logic so that headers are sent before any output to the client.

Your code with this solution

<?php
ob_start();
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
    $error = "Error";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 2024) . " kB<br>";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$new =  "upload/" . $_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"] < 1024)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        $error = "File Error";
    }
  else
    {
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
            $error = "File already exists";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);

      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
        $error = "Uploaded";          
      }
    }
  }
else
  {
  echo "Invalid file";

?> 
<?php
$cid = htmlentities($_GET['id']);
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = "INSERT INTO photo (cid, path) VALUES ('$cid', '$new')";


mysql_select_db('churchinfo');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "<br> Entered data successfully\n";
mysql_close($conn);

?>
</body>
<?php
    ob_clean();
    $url = "?id=" . $cid  . "&res=" . $error;
    header('Location: photo.php'.$url);

?>
<script>
 location.href = "photo.php?" + <?php echo $error; ?> ; 
</script>
<br />

</body>
</html>
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
0

place

ob_start();

in top of your php file and try.

ReNiSh AR
  • 2,782
  • 2
  • 30
  • 42
-1

Usually there is some blank space before the headers. You have to remove it. E.g. Blank space can be introduce also with

? > < ? php

If should close and open php tags just if you need to write some html tags

MrDevel
  • 166
  • 2
  • 13