0

im having a problem with my code in uploading and displaying images.. well I am planning to redirect the page after the upload process is done so I used a header function but gave warning and errors and unfortunately failed the upload.. how can I remove it? here's the code..

<?php 

//connect to the database//
$con = mysql_connect("localhost","root", "");
if(!$con)
{
 die('Could not connect to the database:' . mysql_error());
 echo "ERROR IN CONNECTION";
}

 $sel = mysql_select_db("imagedatabase");
if(!$sel)
{
die('Could not connect to the database:' . mysql_error());
echo "ERROR IN CONNECTION";
}
//file properties//

$file = $_FILES['image']['tmp_name']; 

echo '<br />';

 /*if(!isset($file))
    echo "Please select your images";

else
{
 */for($count = 0; $count < count($_FILES['image']); $count++)
{
//$image = file_get_contents($_FILES['image']['tmp_name']);
$image_desc[$count] = addslashes($_POST['imageDescription'][$count]);
$image_name[$count] = addslashes($_FILES['image]']['name'][$count]); echo '<br \>';
$image_size[$count] = @getimagesize($_FILES['image']['tmp_name'][$count]);
$error[$count] = $_FILES['image']['error'][$count];

if($image_size[$count] === FALSE  || ($image_size[$count]) == 0)
    echo "That's not an image";
else
{

// Temporary file name stored on the server
 $tmpName[$count]  = $_FILES['image']['tmp_name'][$count];

  // Read the file
   $fp[$count]   = fopen($tmpName[$count], 'r');
   $data[$count] = fread($fp[$count], filesize($tmpName[$count]));
   $data[$count] = addslashes($data[$count]);
   fclose($fp[$count]);


 // Create the query and insert
 // into our database.

 $results = mysql_query("INSERT INTO images( description, image) VALUES                 ('$image_desc[$count]','$data[$count]')", $con);

    if(!$results)
    echo "Problem uploding the image. Please check your database";  
   //else 
   //{
      echo "";
    //$last_id = mysql_insert_id();
    //echo "Image Uploaded. <p /> <p /><img src=display.php?    id=$last_id>";
    //header('Lcation: display2.php?id=$last_id');
        }
     //}
}


mysql_close($con);
header('Location: fGallery.php');
?>

the header function supposedly directs me to another page that would make a gallery.. here is the code..

<?php

//connect to the database//
    mysql_connect("localhost","root", "") or die(mysql_error());
    mysql_select_db("imagedatabase") or die(mysql_error());

    //requesting image id




    $image = mysql_query("SELECT * FROM images ORDER BY id DESC");


    while($row = mysql_fetch_assoc($image))
    {
         foreach ($row as $img) echo '<img src="img.php?id='.$img["id"].'">';
    }

    mysql_close();


?>

I have also a problem with my gallery .. some help will be GREAT! THANKS! :D

SimonCode
  • 77
  • 2
  • 3
  • 10
  • Please include the errors you got and if you have problems with your gallery you'll have to explain those problems. – Runar Jørgensen Jan 06 '12 at 15:07
  • 2
    possible duplicate of [Headers already sent by PHP](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – mario Jan 06 '12 at 15:07
  • @RunarJørgensen i had the error in my gallery that it doesnt display any images stored inside the database I specified.. sorry – SimonCode Jan 06 '12 at 15:31
  • 1
    @SimonCode i updated my post with the solution for the gallery. – Runar Jørgensen Jan 06 '12 at 15:38
  • @rekire I approved your answer.. :D which part did I not accept it? You are a great debugger.. you always comment on my posts. :D Thanks so much! :D – SimonCode Jan 07 '12 at 17:49
  • Please do not down vote on my posts/questions .. I hope you understand that I am new to this and still tries to learn.. I know I might have wasted your time but I appreciate that effort you do to help a newbie.. everyone goes through this.. Im no longer allowed to ask question for the moment.. and it will hinder me to learn more about what I'm working on with right now.. Thanks for understanding. – SimonCode Jan 07 '12 at 17:51

3 Answers3

4

The header() function must be called before any other echo or die calls which produce output.

You may could buffer your outputs if you need the output, but in your case it makes no difference because the output will never be shown to the user. The browser will read the redirect and navigate to the second page.


<?php 

//connect to the database//
$con = mysql_connect("localhost","root", "");
if(!$con) {
 // this output is okay the redirect will never be reached.
 die('Could not connect to the database:' . mysql_error());
 // remember after a die this message will never be shown!
 echo "ERROR IN CONNECTION";
}

 $sel = mysql_select_db("imagedatabase");
if(!$sel) {
die('Could not connect to the database:' . mysql_error());
echo "ERROR IN CONNECTION"; // same here with the die!
}
//file properties//

$file = $_FILES['image']['tmp_name']; 

// OUTPUT
// echo '<br />';

// removed out commented code

for($count = 0; $count < count($_FILES['image']); $count++)
{
$image_desc[$count] = addslashes($_POST['imageDescription'][$count]);
$image_name[$count] = addslashes($_FILES['image]']['name'][$count]);
// OUTPUT
// echo '<br \>';
$image_size[$count] = @getimagesize($_FILES['image']['tmp_name'][$count]);
$error[$count] = $_FILES['image']['error'][$count];

if($image_size[$count] === FALSE  || ($image_size[$count]) == 0)
// you may better use a die if you want to prevent the redirection
    echo "That's not an image";
else
{

// Temporary file name stored on the server
 $tmpName[$count]  = $_FILES['image']['tmp_name'][$count];

  // Read the file
   $fp[$count]   = fopen($tmpName[$count], 'r');
   $data[$count] = fread($fp[$count], filesize($tmpName[$count]));
   $data[$count] = addslashes($data[$count]);
   fclose($fp[$count]);


 // Create the query and insert
 // into our database.

 $results = mysql_query("INSERT INTO images( description, image) VALUES                 ('$image_desc[$count]','$data[$count]')", $con);

    if(!$results) // use die
    echo "Problem uploding the image. Please check your database";  
// OUTPUT
//      echo "";
        }
}


mysql_close($con);
header('Location: fGallery.php');
?>

Above I marked every output for you and also removed all outcomments lines.

rekire
  • 47,260
  • 30
  • 167
  • 264
  • 1
    It doesn't have to be called before the die function. If the die function is called the script terminates and the header function will never run. – Runar Jørgensen Jan 06 '12 at 15:09
  • how can I redirect the page to the next after a successful upload? is it only through the use of header functions? or is there another way? – SimonCode Jan 06 '12 at 15:30
  • 1
    As far I know there are 3 possibilities, where `header()` is IMHO the best one, also redirecting using html `` and using javascript `location.replace("url")` or `location.href="url"`. How i told you already remove all outputs like `echo` before the header statement. You could also use the ob_start() function see ror_master link. – rekire Jan 06 '12 at 15:36
  • @rekire i already figured out the errors.. but thanks for the help.. you are a great debugger.. no wonder you have a good reputation already! :D – SimonCode Jan 07 '12 at 15:51
  • @RunarJørgensen thanks for your help.. I somehow figured out already my mistakes.. sorry for my lame questions.. im a total newbie in php compared to all of you.. :D THANKS AGAIN! – SimonCode Jan 07 '12 at 15:53
2

You've got a header error because you printed out <br /> before the header function. In order to use the header function you can't print out any information before it. That's why you're getting the error.

Regarding your gallery the foreach loop is unnecessary. You can change the code to this:

while($row = mysql_fetch_assoc($image)) {
     echo '<img src="img.php?id='.$row["id"].'">';
}
Runar Jørgensen
  • 564
  • 3
  • 13
  • Please show us what's being printed out before the warning. If you get "ERROR IN CONNECTION" something's wrong with your database connection. – Runar Jørgensen Jan 06 '12 at 15:29
1

You can use ob_start() to get data in buffer.

rekire
  • 47,260
  • 30
  • 167
  • 264
Manish Shrivastava
  • 30,617
  • 13
  • 97
  • 101