当我按下“提交”按钮时,页面会重新加载,但不会将我发送到“ upload.inc.php”文件。单击提交按钮后,我只是到达index.php文件,没有任何反应。
我复制了代码,并在另一个文件中尝试了html部分,然后在这里工作了。因此,该错误必须在index.php文件的其余部分中。我已经在“ upload.inc.php”文件的开头添加了echo,它并没有改变任何东西。因此该错误在index.php文件中的某个位置,但是我找不到它。我请求您的帮助!我搜索了4-5个小时,却没有找到。
Index.php(html部分)
<form action="includes/upload.inc.php" method='post' enctype='multipart/form-data'>
<input type='file' name='file'>
<input type='hidden' name='userId' value="<?php echo $id; ?>" >
<button type='submit' name='submit'>UPLOAD</button>
</form>
Index.php(完整文件)
<?php
require "header.php";
require "includes/dbh.inc.php";
?>
<main>
<?php
if (isset($_GET['login'])) {
//Login Bereich der Hilfe suchenden
if ($_GET['login'] == "successHelpSeeker") {
echo "<p> Hello help seeker. </p>";
}
//Login Bereich der Helfenden
elseif ($_GET['login'] == "successHelper") {
echo "<p> Hello Helper.</p>";
}
//Login Bereich für beide gleich
//Profilbild
$id = $_SESSION['userId'];
$sqlImg = "SELECT * FROM profileimg WHERE userid='$id'";
$resultImg = mysqli_query($conn, $sqlImg);
$rowImg = mysqli_fetch_assoc($resultImg);
echo "<div>";
//existiert schon ein Profilbild? bei 0 ja bei 1 nein
if ($rowImg['status'] == 0) {
echo "<img src='uploads/profile".$id.".jpg'>";
} else{
echo "<img src='uploads/profiledefault.jpg'>";
}
echo "<p>".$row['userid']."</p>";
echo "</div>";
//enctype specifies how the form data should be encoded --> ?>
<form action="includes/upload.inc.php" method='enctype='multipart/form-data'>
<input type='file' name='file'>
<input type='hidden' name='userId' value="<?php echo $id; ?>" >
<button type='submit' name='submit'>UPLOAD</button>
</form>
<?php
}
if(isset($_SESSION['userId'])){
echo '<p>You are logged in!!</p>';
}
else{
echo '<p>You are logged out!</p>';
}
?>
</main>
通常它应该将选择的文件发送到upload.inc.php文件,但它只是重新加载页面而不发送信息。
非常感谢您的帮助!
upload.inc.php:
<?php
session_start();
require 'dbh.inc.php';
$id = $_POST['userId'];
if (isset($_POST['submit'])) {
$file = $_FILES['file'];
$fileName = $file['name'];
$fileTmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileError = $file['error'];
$fielType = $file['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png');
if (in_array($fileActualExt, $allowed)) {
if ($fileError === 0) {
if ($fileSize < 10000000) {
$fileNameNew = "profile".$id.".".$fileActualExt;
$fileDestination = '../uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
$sql = "UPDATE profilimg SET status=0 WHERE userid= '$id';";
$result = mysqli_query($conn, $sql);
header("Location: ../index.php?uploadsuccess");
}
} else {
echo "Your file is too big. Please upload a file which isn't bigger than 10 mb.";
}
} else {
echo "There was an error uploading your file.";
}
} else {
echo "Please upload only jpg, jpeg, png or pdf files.";
}
疯狂的事情是,我将“动作”更改为一个几乎没有内容的新文件。提交按钮仍然不会将我发送到新文件,而只是重新加载页面。这可能与我正在处理的会话有关吗? :O
答案 0 :(得分:-1)
查看index.php页面 method ='enctype ='mltipart / form-data'
<form action="includes/upload.inc.php" method='enctype='multipart/form-data'>
<input type='file' name='file'>
<input type='hidden' name='userId' value="<?php echo $id; ?>" >
<button type='submit' name='submit'>UPLOAD</button>
</form>
<?php
}