将上传的图像保存到我的文件时出现问题

problem with saving uploaded image to my file

我可以将图像文件的名称上传到我的数据库,但是它不会保存到我的文件中

我之前在其他系统上尝试过这段代码,效果很好。我不确定为什么它在我当前的系统上不起作用。

在这个系统中,我需要以相同的形式提交图像以及其他输入(其他输入,如姓名、地址、日期等)。

我的表单代码:

<form method = "post" action="insert.php">

<label for="name">Full Name</label>
  <input type="hidden" name="name" value="<?php echo $_SESSION['name']; ?>">

<label for="address">Address</label>
    <textarea id="address" name="address" placeholder="" style="height:100px" class = "form control"></textarea>

//other input field

<label for="picture">Picture</label>
  <form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="hidden" name="picture" value="<?php echo $print[0]+1;?>">

<label for="date">Date & Time</label>
  <input type="datetime" name="date" value="<?php date_default_timezone_set("countryname"); echo date("Y-m-d h:i:s");?>">

<input type="submit" value="Submit" name="submit">

 </form> 
</form>

我的upload.php:

    $target_file = basename($_FILES["fileToUpload"]["name"]);
    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
    $directory = "upload/";
    $uploadOk = 1;

    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
    // Allow certain file formats
    if($imageFileType != "jpg") {
        echo "Sorry, only JPG files are allowed.";
        $uploadOk = 0;
    }

    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } 
    else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $directory)) {
            echo "<script>alert('Your file has successfully been uploaded.'); window.location='insert.php';</script>";
        } 
        else {
            echo "<script>alert('Sorry, there was an error uploading your file.'); window.location='insert.php';</script>";

注意:<?php echo $print[0]+1;?>是显示db中自增的最后一个值

你能指出我做错了什么以及解决方法吗?因为我已经做了很多试验和错误,但仍然找不到答案。非常感谢您的宝贵时间。

您应该将 enctype 添加到您的 html 表单中

 <form method="post" action="insert.php" enctype="multipart/form-data">

避免将 <form> 放在另一个 <form> 中,这样是行不通的。上传和插入同时进行,否则需要上传ajax,然后在提交表单时发送路径

<form method = "post" action="upload-and-insert.php" enctype="multipart/form-data">

    <label for="name">Full Name</label>
    <input type="hidden" name="name" value="<?php echo $_SESSION['name']; ?>">

    <label for="address">Address</label>
    <textarea id="address" name="address" placeholder="" style="height:100px" class = "form control"></textarea>

//other input field

    <label for="picture">Picture</label>
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="hidden" name="picture" value="<?php echo $print[0]+1;?>">

    <label for="date">Date & Time</label>
    <input type="datetime" name="date" value="<?php date_default_timezone_set("countryname"); echo date("Y-m-d h:i:s");?>">

    <input type="submit" value="Submit" name="submit">

</form>