PHP不会将图像文件上传到文件夹

时间:2018-04-10 07:01:55

标签: php

我创建了一个将图像上传到我的数据库并将图像上传到文件夹的功能。

数据库中的网址更改为图片的网址,但该文件未上传到该文件夹​​。

我收到错误:Undefined index: user_image in image.php

这是我的代码:

Image.php

<?php
$edit_row['opzoekImage'] = $_POST["user_image"];
$imgFile = $_FILES['user_image']['name'];
$tmp_dir = $_FILES['user_image']['tmp_name'];
$imgSize = $_FILES['user_image']['size'];

if($imgFile)
{
    $upload_dir = 'user_images/'; // upload directory   
    $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
    $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
    $userpic = rand(1000,1000000).".".$imgExt;
    if(in_array($imgExt, $valid_extensions))
    {           
        if($imgSize < 5000000)
        {
            unlink($upload_dir.$edit_row['opzoekImage']);
            move_uploaded_file($tmp_dir,$upload_dir.$userpic);
        }
        else
        {
            $errMSG = "Sorry, your file is too large it should be less then 5MB";
        }
    }
    else
    {
        $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";        
    }   
}
else
{
    $userpic = $edit_row['opzoekImage']; // old image from database
}
?>

New.php

<?php 
    if(isset($_POST["submit"]) && isset($_GET['website_naam']) && isset($_GET['tbl_name']) && $_GET['tbl_name'] == "tblOpzoek") {
    include('config.php');
    $website_naam = $_GET['website_naam'];
    $sqlWebsiteId =  "SELECT websiteId FROM tblWebsite WHERE websiteNaam = '$website_naam'";
    if($db->query($sqlWebsiteId) != "") {
        $resultWebsiteId = $db->query($sqlWebsiteId);
        if ($resultWebsiteId->num_rows > 0) {
            // output data of each row
            while($row = $resultWebsiteId->fetch_assoc()) {
                $websiteId = $row["websiteId"];
            }
            include('image.php');
    $sqlToevoegenType = "INSERT INTO tblOpzoek (websiteId, opzoekName, opzoekValue, opzoekImage) VALUES ('".$websiteId."', '".$_POST["typeNaamToevoegen"]."', '".$_POST["typeWaardeToevoegen"]."', '".$userpic."')";
    if($db->query($sqlToevoegenType) === TRUE) {
        header('Location: ' . $_SERVER['HTTP_REFERER']);
        } else {
        echo "<script type= 'text/javascript'>alert('Error: " . $sqlToevoegenType . "<br>" . $db->error."');</script>";
        }
        } else {
            echo "0 results";
        }
        } 
    }
?>

我的表单 很高兴注意到我的文件输入name="user_image"

有更多表格
<!-- Type toevoegen-->
<div class="modal fade" id="addType" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
     <form action="php/new.php?website_naam=<?php echo $websiteNaam ?>&tbl_name=tblOpzoek" method="post" id="formTypeToevoegen">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Type toevoegen</h4>
      </div>
      <div class="modal-body">
        <div class="form-group">
            <label>Type Naam</label>
            <input type="text" id="typeNaamToevoegen" name="typeNaamToevoegen" class="form-control" >
        </div>
        <div class="form-group">
            <label>Type Waarde</label>
            <input type="text" id="typeWaardeToevoegen" name="typeWaardeToevoegen" class="form-control" >
        </div>
        <div class="form-group">
            <label>Type Afbeelding</label>
            <input class="input-group" type="file" id="videoUploadFile" name="user_image" accept="image/*" />
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Sluiten</button>
        <button type="submit" name="submit" class="btn btn-primary">Gegevens opslaan</button>
      </div>
    </form>
    </div>
  </div>
</div>

谢谢你的时间!

2 个答案:

答案 0 :(得分:2)

您需要将enctype属性添加到<form>代码

<form enctype="multipart/form-data">

答案 1 :(得分:0)

根据您的错误,首先检查您在输入标记中定义的名称,因为如果您在输入标记中定义了不同的名称并在$ _FILES中定义了不同的名称,那么它会显示错误。

因此,在输入标记和$ _FILES中定义相同的名称,例如

$ _FILES [&#39; user_image&#39;] [&#39;名称&#39;];

如果以上是正确的,那么检查是否添加了enctype =&#34; multipart / form-data&#34;在形式与否。