【问题标题】:File upload with mysql don't works用mysql上传文件不起作用
【发布时间】:2020-04-24 13:25:27
【问题描述】:

我写了一个简单的上传系统,但上传系统不起作用。我希望你能帮帮我!

我的php代码:

if (isset($_POST['taskAdd'])) {
    move_uploaded_file($_FILES['taskFile1']['tmp_name'], '../files/' . $_FILES['taskFile1']['name']);
    $stmt = $con->prepare("INSERT INTO tasks (taskFile1) VALUES (:taskFile1)");
    $stmt->execute();
            header("Location: /index/");
}

错误信息:

Warning: move_uploaded_file(../files/login.html): failed to open stream: No such file or directory in C:\xampp\htdocs\functions\taskAdd.php on line 13

Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpA9F3.tmp' to '../files/login.html' in C:\xampp\htdocs\functions\taskAdd.php on line 13

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':taskFile1)' at line 1 in C:\xampp\htdocs\functions\taskAdd.php:15 Stack trace: #0 C:\xampp\htdocs\functions\taskAdd.php(15): PDOStatement->execute() #1 C:\xampp\htdocs\tasks\add\index.php(7): include('C:\\xampp\\htdocs...') #2 {main} thrown in C:\xampp\htdocs\functions\taskAdd.php on line 4

HTML 表格:

<form enctype="multipart/form-data" method='post' name='taskAdd'>

                                            <div class="form-group">
                                            <label for="taskFile1">Hochladen1:</label>
                                            <input type="file" class="form-control-file" name="taskFile1" id="taskFile1">
                                            </div>
</form>

【问题讨论】:

  • 你需要READ THE MANUAL
  • tasks表中taskFile1字段的数据类型是什么?您是否尝试将文件信息(名称、路径等)或完整文件作为二进制文件存储在数据库中?
  • taskFile1 的数据类型是 varchar,我将只保存数据库中的名称和文件夹“file”中的文件。
  • 你应该在准备之后绑定execute()
  • 发布您的完整 HTML 表单

标签: php mysql xampp


【解决方案1】:

不要将'../files/' . $_FILES['taskFile1']['name']作为第二个参数传递给move_uploaded_file,你需要传递:

dirname(__FILE__) . '/files/' . $_FILES['taskFile1']['name']

或更好,

dirname(__FILE__) . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . $_FILES['taskFile1']['name']

另外,您必须确保“../files/”目录的权限设置正确。

【讨论】:

    【解决方案2】:

    1)https://www.w3schools.com/php/php_file_upload.asp

    2)$name=$_FILES[$v1]['name'];

        $tmpname=$_FILES["taskFile1"]['tmp_name'];//filetoupload is a name attribute of file input tag.
    
        $target_dir = "uploads/"; //folder name where your files will be stored. create this folder inside "file_upload_api" folder
        $target_file = $target_dir.$name ;
        move_uploaded_file($tmpname,$target_file)
    

    //并将文件名插入数据库中以进行进一步操作

    //在html中添加按钮进行上传

    【讨论】:

      【解决方案3】:
      1. 您的插入语句不正确。
      2. 您没有提供插入参数。即绑定

      应该是:

      $stmt = $con->prepare("INSERT INTO tasks (taskFile1) VALUES (?)");
      $stmt->bind_param("s", $_FILES['taskFile1']['name']);
      $stmt->execute();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-27
        • 2014-08-12
        • 2014-01-09
        • 1970-01-01
        • 1970-01-01
        • 2013-03-17
        • 2013-12-09
        • 2015-02-11
        相关资源
        最近更新 更多