【问题标题】:problem with saving uploaded image to my file将上传的图像保存到我的文件的问题
【发布时间】:2019-12-20 21:13:53
【问题描述】:

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

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

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

我的表单代码:

<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>

我的上传.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>";

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

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

【问题讨论】:

  • “我的表单代码:” - 您不能将表单相互嵌套,这是无效的 HTML。首先去解决这个问题。
  • 您应该在表单元素中提及enctype="multipart/form-data"。
  • @Gulshan 有提到,但是在一个form里面一个form
  • @kerbholz 为什么在form 中使用form?您可以管理保存数据和上传图像,有条件地使用相同的功能。
  • @Gulshan 我不要在form中使用form,我不是OP。

标签: php html mysql upload


【解决方案1】:

避免在另一个&lt;form&gt; 中使用&lt;form&gt;,它不会起作用。上传和插入同时进行,否则需要用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>

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-10-27
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多