如何删除标头错误

时间:2012-01-06 15:04:39

标签: php mysql database

我的代码在上传和显示图片时遇到问题..好吧我计划在上传过程完成后重定向页面,所以我使用了标题功能,但发出了警告和错误,不幸的是上传失败..如何我可以删除吗?这是代码..

<?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');
?>

标题函数据说会将我引导到另一个可以创建图库的页面..这里是代码..

<?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();


?>

我的画廊也有问题......一些帮助会很棒!谢谢! :d

3 个答案:

答案 0 :(得分:4)

必须在产生输出的任何其他header()echo调用之前调用die函数。

如果需要输出,您可以缓冲输出,但在您的情况下,它没有任何区别,因为输出永远不会显示给用户。浏览器将读取重定向并导航到第二页。


<?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');
?>

上面我为您标记了所有输出,并删除了所有输出行。

答案 1 :(得分:2)

由于您在标题功能之前打印了<br />,因此您收到了标题错误。要使用标题功能,您无法在其之前打印出任何信息。这就是您收到错误的原因。

关于你的画廊,foreach循环是不必要的。您可以将代码更改为:

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

答案 2 :(得分:1)

您可以使用ob_start()在缓冲区中获取数据。