首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在php文件中追加数据

如何在php文件中追加数据
EN

Stack Overflow用户
提问于 2018-09-28 14:44:06
回答 2查看 53关注 0票数 1

以前,我使用php将表单数据附加到json文件中。目前,我正在处理php文件,而不是json文件,它有两个参数,如图图像url链接所示,它显示给用户。

我的表格是

代码语言:javascript
运行
复制
<form action="process.php" method="POST">
	Name:<br>
	<input type="text" name="name">
	<br><br/>
	Download Url:<br>
	<input type="text" name="downloadUrl">
	<br><br>
	  

	  
	  
	<input type="submit" value="Submit">
</form>

我的php文件是

代码语言:javascript
运行
复制
{"downloadurl":[


{"name":"भाद्र ६ : LCR Series",
"downloadurl":"https://drive.google.com/uc?export=download&id=1In76AN2Y5_qXV5ucXDXWx1PTKTTIvD3d"

},

{"name":"भाद्र ६ : LCR Parallel",
"downloadurl":"https://drive.google.com/uc?export=download&id=1R9Ia4X12JZMsTn_vF6z443K6wKI2Rfeu"

}


]}

如何使用追加的新数据,以便当单击submit按钮时,在上述php文件的基础上添加新数据,新文件将被添加到

代码语言:javascript
运行
复制
    {"downloadurl":[


 {"name":"भाद्र ६ : New appended Data",
    "downloadurl":"This is new Text added on Top"

    },
    
    

    {"name":"भाद्र ६ : LCR Series",
    "downloadurl":"https://drive.google.com/uc?export=download&id=1In76AN2Y5_qXV5ucXDXWx1PTKTTIvD3d"

    },

    {"name":"भाद्र ६ : LCR Parallel",
    "downloadurl":"https://drive.google.com/uc?export=download&id=1R9Ia4X12JZMsTn_vF6z443K6wKI2Rfeu"

    }


    ]}

在顶部,它将显示给用户。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-28 14:51:17

从你的问题来看,我认为这将解决你的问题。我使用array_unshift()是因为您使用的短语on top使我认为您希望在现有数据之前显示数据,如果这不正确,请将array_unshift()替换为array_push(),以便它在数据之后添加,或者参见解决方案2。

解决方案1:

代码语言:javascript
运行
复制
<?php

    //This is where your JSON file is located
    $jsonFile = '/path/to/json/file';

    //Get the contents of your JSON file, and make it a useable array.
    $JSONString = json_decode( file_get_contents( $jsonFile ), true );

    //This is the new data that you want to add to your JSON
    $newData = array(
            //Your data goes here
        );

    //Add the new data to the start of your JSON
    array_unshift($existingData, $newData);

    //Encode the new array back to JSON
    $newData = json_encode( $existingData, JSON_PRETTY_PRINT );

    //Put the JSON back into the file
    file_put_contents($jsonFile, $newData);

?>

解决方案2:

代码语言:javascript
运行
复制
<?php

    //This is where your JSON file is located
    $jsonFile = '/path/to/json/file';

    //This is the new data that you want to add to your JSON
    $newData = array(
            //Your data goes here
        );

    //Encode the new data as JSON
    $newData = json_encode( $newData, JSON_PRETTY_PRINT );

    //append the new data to the existing data
    file_put_contents( $jsonFile, $newData, FILE_APPEND );

?>
票数 0
EN

Stack Overflow用户

发布于 2018-09-28 14:50:01

我不明白为什么要在PHP文件的末尾追加文本,但是可以使用file_put_contents()FILE_APPEND标志。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52557730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档