您好,开发人员。
为了在PHP中创建自动的Drag n'Drop函数来上传文件,保存第一个文件后,我陷入了数据更新的困境。实际上,我在数据库中存储了一个JSON表,其中包含文件的名称,路径和类型。
看起来像这样:{
"1": {
"name": "1",
"path": "/files/project/customer1/pictures/",
"type": "jpeg"
}
}
但是,如果现在我添加另一个文件,我找不到找到按照我的JSON结构添加它的解决方案。
我希望它遵循此逻辑:{
"1": {
"name": "img1",
"path": "/files/project/customer1/pictures/",
"type": "jpeg"
},
"2": {
"name": "img2",
"path": "/files/project/customer1/pictures/",
"type": "jpeg"
}
}
这是我的条件。我在PHP中使用MEDOO库,因此在update function。]
<< [before function检索“。”之后的所有内容。因此,请恢复文件类型。<?php
if ($error == "") {
echo $_FILES["$name"]["name"] . " a bien été téléchargé";
$jsonOrig = '';
$jsonOrig = $database->get($table, 'attachment', ['id' => $table_id]);
$database->update(
$table,
['attachment' => Core::jsonify(
$jsonOrig,
['name' => $this->before('.', $_FILES["$name"]["name"]), 'path' => '/files/' . $path . '/', 'type' => $ext],
1
)],
['id' => $table_id]
);
}jsonify函数允许您自动生成JSON。<?php
public static function jsonify($jsonOrig = array(), $jsonAdd, $where = "")
{
header('Content-Type: application/json');
if (!is_array($jsonOrig)) {
$jsonOrig = json_decode($jsonOrig, true);
}
if (!is_array($jsonAdd)) {
$jsonAdd = json_decode($jsonAdd, true);
}
if (empty($where)) $jsonOrig[] = $jsonAdd;
else $jsonOrig[$where] = $jsonAdd;
return json_encode($jsonOrig, JSON_PRETTY_PRINT);
}这里是一次使用的功能
// parameter one = input file name
// parameter two = The path where the file will be uploaded
// parameter three = table name database
// parameter four = id where add the json file in the database
$Files->addFile('file', 'projets/client2/photos', 'articles', 54);谢谢您的帮助。