mythri Posted September 4, 2021 Share Posted September 4, 2021 I am uploading json files to my directory called upload/source and the file name in database. When i click on edit, the json file should open in textarea or or in any editor so that i can edit the contents and save. my json file is like this src.json { "sources": [ { "id": "1", "event": "labels", "display": "Label Settings", "resultevent": "printthis" } ] } i got the file contents into textarea to edit something like this <form name="123" action="" method="post"> <table class="table m-0"> <tbody> <?php $l1 = $con->prepare("SELECT sid, src_name FROM sources WHERE sid=?"); $l1->execute([1]); foreach($l1 as $r1) { $file = "uploads/sources/".$r1['src_name']; //Path to your *.txt file $contents = file($file); $string = implode($contents); ?> <tr> <td><?php echo $r1['src_name']; ?></td> <td><textarea name="file"><?php echo $string; ?></textarea></td> <input type="hidden" name="id" value="<?php echo $r1['sid']; ?>" /> </tr> <?php } ?> <tr><td colspan="2"><input type="submit" name="save" class="btn" /></td></tr> </tbody> </table> </form> But i am not getting how to save the edited contents as src.json(same name) . Can somebody suggest me how to do it Quote Link to comment https://forums.phpfreaks.com/topic/313658-editing-uploaded-file-contents-and-saving-it/ Share on other sites More sharing options...
Solution mythri Posted September 4, 2021 Author Solution Share Posted September 4, 2021 I got it resolved! Quote Link to comment https://forums.phpfreaks.com/topic/313658-editing-uploaded-file-contents-and-saving-it/#findComment-1589636 Share on other sites More sharing options...
requinix Posted September 6, 2021 Share Posted September 6, 2021 For anybody else who wanted to know what the answer was, 1. Form has to indicate what the sid was so that the page receiving the new file contents knows which file it has to update. Do not give the src_name. That is not secure. sid is the way. 2. Write code that uses $_POST to get sid and new file contents, file_put_contents with the appropriate filename, ???, profit. Separately: loops are used when there are multiple things to work with. That query does not give multiple things to work with. There should not be a loop. Quote Link to comment https://forums.phpfreaks.com/topic/313658-editing-uploaded-file-contents-and-saving-it/#findComment-1589680 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.