我使用该表格直接将图像存储在数据库中,现在,我想学习如何将它们存储在一个文件夹中,并将数据存储在数据库中。
此刻我收到此错误Notice: Undefined index: uploaded_file in
,我真的不明白为什么。请一些F1 :)
Html表格:
<form action="ad_cont.php" method="POST" class="add_contact" name="add_contact" enctype="multipart/form-data">
<input type="file" name="uploaded_file" multiple required>
<input type="submit" value="Upload" class="button">
</form>
Php脚本:
$img_path = "images/avatar";
$img_path = $img_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $img_path)) {
mysqli_query($conn,"INSERT INTO `uploads` (filename,path)
VALUES ('".$_FILES['uploaded_file']['tmp_name']."','".$img_path."')");
答案 0 :(得分:3)
您必须在表单中添加enctype='multipart/form-data'
才能上传文件。
<form action="ad_cont.php" method="POST" class="add_contact" enctype="multipart/form-data" name="add_contact">
<input type="file" name="uploaded_file" multiple required>
<input type="submit" value="Upload" class="button">
</form>
像这样更改php代码。
$img_path = "images/avatar";
$img_path = $img_path . basename( $_FILES['uploaded_file']['name']);
$img_name= $_FILES['uploaded_file']['tmp_name'];
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $img_path)) {
mysqli_query($conn,"INSERT INTO `uploads` (filename,path)
VALUES ('".$img_name."','".$img_path."')");
尝试更改此表单的代码,并希望它能够正常工作。
答案 1 :(得分:0)
要将文件上传到服务器,您需要在enctype='multipart/form-data'
<form>
<form enctype="multipart/form-data" action="ad_cont.php" method="POST" class="add_contact" name="add_contact" >
..
..
</form>