我的代码假设在上传成功后跳转到下一页但是我收到了这个警告。 “
警告:无法修改标头信息 - 已在C:\ XAMP \ xampp \ htdocs \ gallery \ uploader3.php中发送的标头(输出从C:\ XAMP \ xampp \ htdocs \ gallery \ uploader3.php:25开始)第51行
这是代码:
<html>
<head>
<title> Sample1 - File Upload on Directory </title>
</head>
<body>
<div align="center">
<form action="uploader3.php" method="post" enctype="multipart/form-data" >
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Create an Album (limited to 10 images): <br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<br />
<input type="submit" value="Upload File" />
</form>
</div>
<?php
$target_path = "uploads1/";
if(!file_exists($target_path))
{
if(!mkdir($target_path))
{
die ("could not create the folder");
}
}
else
{
for($count = 0; $count < count($_FILES['uploadedfile']); $count++)
{
$target_path = $target_path . basename( $_FILES['uploadedfile']['name'][$count]);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$count], $target_path))
{
/*echo "The file ". basename( $_FILES['uploadedfile']['name'][$count]).
" has been uploaded";*/
}
else
{
echo "There was an error uploading the file, please try again!";
}
}
}
header('Location: index.html');
?>
</body>
</html>
我该如何解决这个问题?谢谢你好! :d
答案 0 :(得分:1)
在页面顶部写下ob_start()
在exit()
header()
答案 1 :(得分:1)
您的代码流程错误。您的PHP脚本在加载该页面后立即执行,而不是在用户提交表单
时执行您需要将<?php ?>
(包括那些标记)之间的代码移动到uploader3.php,就像表单目标操作设置为的那样。
或者,您可以将脚本放在页面顶部并检查表单数据是否已发送,并从表单标记中删除action属性,以便它只是提交回自身或更改它以使其指向自身
实施例
<?php
//Check that you have some post data
if( isset($_POST['submit']) )
{
$target_path = "uploads1/";
if(!file_exists($target_path))
{
if(!mkdir($target_path))
{
die ("could not create the folder");
}
}
else
{
for($count = 0; $count < count($_FILES['uploadedfile']); $count++)
{
$target_path = $target_path . basename( $_FILES['uploadedfile']['name'][$count]);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$count], $target_path))
{
/*echo "The file ". basename( $_FILES['uploadedfile']['name'][$count]).
" has been uploaded";*/
}
else
{
echo "There was an error uploading the file, please try again!";
}
}
}
header('Location: index.html');
}
?>
<html>
<head>
<title> Sample1 - File Upload on Directory </title>
</head>
<body>
<div align="center">
<form method="post" enctype="multipart/form-data" >
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Create an Album (limited to 10 images): <br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<br />
<input type="submit" name="submit" value="Upload File" />
</form>
</div>
</body>
</html>
答案 2 :(得分:0)
错误意味着发送标题(header('location')
,session_start
等)的某些功能无法执行此操作,因为它已全部发送。
大多数情况下,这是因为如果您开始输出内容,则会隐式发送标题。常见的错误是
session_start
)只需查找警告所涉及的行,查看调用的函数,并确保在那里需要该函数,如果它发送标题,请确保之前不发送内容。
现在你可以像@diEcho建议的那样使用输出缓冲来解决这个问题,但我不认为你在所有情况下都想要这个。 (我很快就会认为大多数情况都不需要它。)
答案 3 :(得分:-1)
在开头写OB_START
。这将删除警告,但您的代码不适合在第二页上重定向。