如何编写php子类将数据和文件插入数据库?

时间:2015-02-23 15:59:29

标签: php mysql class file-upload

我正在使用this method尝试允许用户在ajax表单中上传文件。我已经在iframe中隔离了表单的文件部分,因为表单的一些JS和文件上传系统发生了冲突。该系统现在可以运行 - 有一个关键的例外。这是一个美丽的系统,但它似乎我的能力有限,相当复杂。它将文件成功保存到服务器磁盘,但似乎我必须编写一个子类,使其将文件作为blob保存到mysql数据库中。我一直试图让这个工作几天没有成功。我对课程并不熟悉 - 虽然已经阅读了很多(并且没有像我想的那样理解)。

这是我尝试的子类条目的代码,以及文件上传脚本在提交时立即与之对话的位置。

<?php
/*
 * jQuery File Upload Plugin PHP Example 5.14
 * https://github.com/blueimp/jQuery-File-Upload
 *
 * Copyright 2010, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/MIT
 *
 */
error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');

class MyUploadHandler extends UploadHandler {
    /*
    * Maintain our database:
    * For each uploaded file, add an entry to the tbl_xxx
    */
    protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index = null, $content_range = null) {
        // this does what would have automatically been done before we subclasses anything
        $file = parent::handle_file_upload($uploaded_file, $name, $size, $type, $error, $index, $content_range);

        // Now we add some extra processing if the file uploaded with no problems
        if(empty($file->error)) {
            // include the funky stuff

            // Grab the includes
            include '../conf/Funcs.php';
            include '../conf/DBconfig.php';

            // Grab critical data
            $appID = $_POST['appID'];
            $uaID = $_POST['uaID'];
            $uploaders_uID = $_POST['uID'];
            $applicationKey = $_POST['applicationKey'];
            $token = $_POST['token'];
            $dateUploaded = time();

            // Define our arrays which we will be using
            $fileName = array();
            $fileMime = array();
            $fileSize = array();
            $fileData = array();

            // And grab our file data
            $fileName = $_FILES['files']['name'];
            $fileMime = $_FILES['files']['type'];
            $fileSize = $_FILES['files']['size'];
//          $fileData = fopen($_FILES['files']['tmp_name'], "rb");
            $fileData = $_FILES['files']['files[]'];



            foreach ($fileName as $nameitem) {
                foreach ($fileMime as $mimeitem) {
                    foreach ($fileSize as $sizeitem) {
//                      foreach ($fileData as $dataitem) {
                            echo "$nameitem\n";
                            echo "$mimeitem\n";
                            echo "$sizeitem\n";
//                          echo "$dataitem\n";
//           THIS IS WHERE THE DB COMMUNICATION CHUNK
//           WOULD BE GOING IF THIS WORKED.


//                      }
                    }
                }
            }


        }

    return $file;
    }
} // end of class

$upload_handler = new MyUploadHandler($site_params);

// THIS IS THE DB COMMUNICATION PART THAT WILL GO
// AS MARKED UP ABOVE, ONCE THE THING WORKS
//  try {
//      $stmt = $conn->prepare("INSERT INTO $database.appsFiles (`appID`, `uaID`, `uploaders_uID`, `dateUploaded`, `applicationKey`, `fileName`, `fileMime`, `fileSize`, `file`)
//      VALUES
//      (?,?,?,?,?,?,?,?,?)");
        // Bind Values
//      $stmt->bindParam(1, $appID, PDO::PARAM_INT, 11);
//      $stmt->bindParam(2, $uaID, PDO::PARAM_INT, 11);
//      $stmt->bindParam(3, $uploaders_uID, PDO::PARAM_INT, 11);
//      $stmt->bindParam(4, time(), PDO::PARAM_INT, 11);
//      $stmt->bindParam(5, $applicationKey, PDO::PARAM_STR, 8);
//      $stmt->bindParam(6, $fileName, PDO::PARAM_STR, 255);
//      $stmt->bindParam(7, $fileMime, PDO::PARAM_STR, 128);
//      $stmt->bindParam(8, $fileSize, PDO::PARAM_INT, 20);
//      $stmt->bindParam(9, $file, PDO::PARAM_LOB, 5120000);

        // Execute the query
//      $stmt->execute();
//  } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
?>

我知道此时没有数据检查。我将在稍后解决如何将包含所有相关信息的文件添加到数据库中时添加。

非常感谢帮助。

0 个答案:

没有答案