简体   繁体   English

如何清除标题错误

[英]how to remove the header error

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 :D

The header() function must be called before any other echo or die calls which produce output. 必须在生成任何其他echodie调用之前调用header()函数。

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. 在上方,我为您标记了每个输出,还删除了所有注释行。

You've got a header error because you printed out <br /> before the header function. 您遇到标头错误,因为您在标头函数之前打印了<br /> 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. 关于您的画廊,foreach循环是不必要的。 You can change the code to this: 您可以将代码更改为此:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM