php 往json中添加数据,php - 在JSON(PHP)中的其他数据之后添加数据 - SO中文参考 - www.soinside.com...

您好,开发人员。

为了在PHP中创建自动的Drag n'Drop函数来上传文件,保存第一个文件后,我陷入了数据更新的困境。实际上,我在数据库中存储了一个JSON表,其中包含文件的名称,路径和类型。

看起来像这样:{

"1": {

"name": "1",

"path": "/files/project/customer1/pictures/",

"type": "jpeg"

}

}

eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS80WjE0dS5wbmcifQ==

但是,如果现在我添加另一个文件,我找不到找到按照我的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);谢谢您的帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值