我有以下PHP结构:
if ((move_uploaded_file($_FILES['file1']['tmp_name'],$newname))) {
echo "It's done! The file has been saved as: ".$newname;
//Writes the information to the database
$y = mysqli_query($con,"SELECT id FROM files ORDER BY id DESC LIMIT 1") ;
$a = mysqli_fetch_array($y,MYSQL_ASSOC);
$index = (int)$a['id']+1;
$x = mysqli_query($con,"INSERT INTO files (id,title,link) VALUES ('$index', '$title', '$file')") ;
header('Location: upload2.php');
exit();
并且这个结构有一个问题,它没有重定向...我想头文件功能不像我用的那样工作。
我需要为我的代码的最后两行提供替代解决方案,因此如果文件上传成功,我想在数据库中写入信息,然后重定向到页面。
如何实现此功能?
答案 0 :(得分:0)
您已经打印了有关成功上传的句子,因此浏览器会自动设置html标头。您不能两次发送标题,因此重定向根本不起作用。您可能应该将成功的消息作为重定向中的变量传递,并在重定向后稍后在其他页面中使用
if ((move_uploaded_file($_FILES['file1']['tmp_name'],$newname))) {
$message = "It's done! The file has been saved as: ".$newname;
//Writes the information to the database
$y = mysqli_query($con,"SELECT id FROM files ORDER BY id DESC LIMIT 1") ;
$a = mysqli_fetch_array($y,MYSQL_ASSOC);
$index = (int)$a['id']+1;
$x = mysqli_query($con,"INSERT INTO files (id,title,link) VALUES ('$index', '$title', '$file')") ;
header("Location: upload2.php?message=$message");
exit();
您可以使用简单的$GET['message']