Jump to content

Editing uploaded file contents and saving it


mythri
 Share

Go to solution Solved by mythri,

Recommended Posts

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                                

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.