gpt4 book ai didi

php - 如何删除 header 错误

转载 作者:行者123 更新时间:2023-12-01 00:14:11 27 4
gpt4 key购买 nike

我的代码在上传和显示图像时遇到问题。我打算在上传过程完成后重定向页面,所以我使用了 header 函数,但给出了警告和错误,不幸的是上传失败了。怎么办我可以删除它吗?这是代码..

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

header 函数应该将我引导到另一个可以制作画廊的页面..这是代码..

<?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

最佳答案

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

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


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

上面我为您标记了每个输出,还删除了所有注释行。

关于php - 如何删除 header 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8759840/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com