我正在尝试使用在互联网上找到的一些代码将图像文件上传到我的服务器,但它不起作用,我也找不到原因。
<form method="post" action="?p=edit-profil&id=<?php echo($user_id); ?>">
<h2>Profil bearbeiten</h2>
<hr />
<h4>Profilbild ändern</h4>
<p style="margin-top: 5px;">(Zulässige Dateiformate: .png .jpg)</p>
<input type="hidden" name="size" value="1000000" style="padding: 6px;">
<div>
<input type="file" name="image" class="choose-image">
</div>
<div>
<input type="submit" name="add-personal-data" value="Speichern" class="submitbutton">
</div>
</form>
// Get image name
$image = $_FILES['image']['name'];
// image file directory
$target = "img/".basename($image);
$image_insert = 'img/'.$image;
$image_upload_statement = $pdo->prepare("UPDATE beschreibung SET profilbild = '$image_insert' WHERE user_id = '$user_id'");
$image_upload_statement->execute();
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "Image uploaded successfully";
}else{
$msg = "Failed to upload image";
}
所以通常 image_insert 应该是像 img/picture.png 这样的东西,但我只得到 img/并且也没有上传。由于我对这个主题的工作还很陌生,所以我不知道如何解决这个问题。
最佳答案
您的表单标签应具有此属性 enctype="multipart/form-data"
才能将图像发送到 php 文件。
关于php - 图片上传不起作用,我也没有收到错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55885075/