如何编写php子类将数据和文件插入数据库?
我正在使用它来尝试允许用户在ajax表单中上载文件。我在iframe中隔离了表单的文件部分,因为表单的一些JS与文件上传系统发生冲突。该系统现在可以工作了——只有一个关键的例外。这是一个漂亮的系统,但在我看来,它的能力有限,相当复杂。它成功地将一个文件保存到服务器磁盘上,但似乎我必须编写一个子类,使其将文件作为blob保存到mysql数据库中。几天来,我一直在努力让它发挥作用,但都没有成功。我对课程不熟悉——尽管我读了很多书(并没有像我想的那样理解) 这是我试图输入的子类的代码,也是文件上传脚本在提交后立即与之对话的代码如何编写php子类将数据和文件插入数据库?,php,mysql,class,file-upload,Php,Mysql,Class,File Upload,我正在使用它来尝试允许用户在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()); }
?>
我知道目前没有数据检查。当我解决了如何将包含所有相关信息的文件放入数据库时,我会在后面添加这些信息
非常感谢您的帮助。问题是什么?另外,您确定应该将文件存储在数据库中吗?也许您应该将文件存储在文件系统中,并将文件路径存储在数据库中?问题是如何将文件放入数据库中。我的课堂努力有什么问题?是的,它们需要在数据库中,而不是凌乱服务器文件系统。