Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

laksh's avatar
Level 1

How to edit html file on every save

so i am working on a task on clicking a button in list view a file will uploaded to git repository but before uploading the file i want that in that file there is a line in which token have to changed so, i want that on clicking on button it will automatically replace the token stored in database to that file and then uploaded to git

Here's the part of code which i want to replace

		"sign-up-modal" style="display: none;">
     src="https://wd.supracrm.com/sign-up-form?token=<REPLACE_TOKEN>"
        crossorigin="anonymous"></script>

but, i don't have any idea how should i do that

any help will be apperciable

0 likes
58 replies
Tray2's avatar

So basically you want to update a value in a table in the database, then for some reason unknown you want to update a file on your servers files system with that value and then do a git add, git commit and git push?

Why do you want to store something on github everytime a value in the database changes?

laksh's avatar
Level 1

@Tray2 i mean to say a token is stored in database and i want that the file will replace itself by that token everytime we call ajax for commit file on git

Sinnbeck's avatar

@laksh why not just use php to set the token dynamically?

		"sign-up-modal" style="display: none;">
     src="https://wd.supracrm.com/sign-up-form?token={{$token}}"
        crossorigin="anonymous"></script>
laksh's avatar
Level 1

@Sinnbeck in my case there is particular token for every entry on table so i want that whenever i clicked on list view the token related to that id will set there

will it work like that?

Sinnbeck's avatar

@laksh you write code in the controller so send the correct token to the view

laksh's avatar
Level 1

@Sinnbeck but this file is not stored in my view this is a random file which is located into a different folder so how will i set the token in it

Sinnbeck's avatar

@laksh sorry I'm really unsure what you are trying? Any reason for not just having it in a view?

laksh's avatar
Level 1

@Sinnbeck because it is not a view file that file is which i have to upload on git only after replacing token and else there is no work of this file

Sinnbeck's avatar

What happens if two users click the button at once with different tokens? Random who gets the file committed?

laksh's avatar
Level 1

@Sinnbeck no only single user will click button at a time not more than one users

Sinnbeck's avatar

Ok I will give you the steps, but I cannot guarantee that it will be problem free

  1. Give the server git user push privileges
  2. Create a template version of the file
  3. Write some php that loads the template, replaces the token and overwrite the real file
  4. Use this to run git add and git commit and git push https://symfony.com/doc/current/components/process.html
laksh's avatar
Level 1

@Sinnbeck what is the reason behind server git user push privileges and where should i write php to load template and replace token?

Sinnbeck's avatar

@laksh uploading is git push

a file will uploaded to git repository

So you cannot "upload" unless you are allowed to push

And maybe do it either in the store method of that button, or dispatch it to a job?

Sinnbeck's avatar

@laksh what about it? Open the template, replace <REPLACE_TOKEN>with the actual token in the string, and save it to the real file

Sinnbeck's avatar

@laksh example

$content = file_get_contents('/path/to/template.html');
$content = str_replace('<REPLACE_TOKEN>', $token, $content);
file_put_contents('/path/to/real.html', $content);
laksh's avatar
Level 1

@Sinnbeck i dont know why but it's giving error to me after i put path in file_get_contents that failed to open stream even the location is right

Sinnbeck's avatar

@laksh post what you have. Also make sure you have permission to write to the file (the web server user)

laksh's avatar
Level 1

@Sinnbeck here's my code

		public function onUploadGit(){
    
    $id = post()['id'];
    
    $attr = Named::find($id);
    
    $signup_id = $attr['signup_widget_id'];
    
    $signup_widget = Client::find($signup_id);
    
    $token = $signup_widget['token'];
    
    $content = file_get_contents('/supracrm/plugins/bm/brand/clientPortalFiles/index.html');
    
    $content = str_replace('<REPLACE_TOKEN>', $token, $content);
    
    $file = file_put_contents('supracrm/plugins/bm/brand/clientPortalFiles/index.html', $content);
    
    //$file = "plugins/bm/brand/clientPortalFiles/index.html";
    
    $name = $attr['brand'];
    
    $data = array(
        'sha' => file_get_contents("plugins/bm/brand/clientPortalFiles/sha.txt"),
        'message' => 'New File',
        'content' => base64_encode($file),
        'committer' => array(
            'name' => '',
            'email' => '',
        )
    );
    
    $data_string = json_encode($data);
    
    $url = "https://api.github.com/repos/laqsh11/demo/contents/'$name'/index.html";
    
    $ch = curl_init($url);
    
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'User-Agent: Awesome-Octocat-App',
        'Authorization: Bearer ghp_TsBwlcovecD0fnkfhrbfrh408QwUL'
    ));
    
    $result = curl_exec($ch);
    
}

and here's the error which i get

		"file_get_contents(/supracrm/plugins/bm/brand/clientPortalFiles/index.html): failed to open stream: No such file or directory" on line 92 of D:\xampp\htdocs\supracrm\plugins\bm\brand\controllers\Brand.php
Sinnbeck's avatar

@laksh you are giving it a bad path. Just guessing here

$content = file_get_contents(base_path('/supracrm/plugins/bm/brand/clientPortalFiles/index.html')); 
laksh's avatar
Level 1

@Sinnbeck the token is replacing now but file is not uploading into git and also not getting any error

Sinnbeck's avatar

@laksh it seems that you want to upload it with curl? Where did you read about that?

Sinnbeck's avatar

@laksh about uploading files to git. I assume there is some error in that implementation. I have never used it so I cannot really give suggestions

Sinnbeck's avatar

I also assume this path is wrong

file_get_contents("plugins/bm/brand/clientPortalFiles/sha.txt") 
laksh's avatar
Level 1

@Sinnbeck no i changed that path but it doesn't changes anything after i changed the path

Sinnbeck's avatar

@laksh show what you have now. Did you change the content like I showed you?

laksh's avatar
Level 1

@Sinnbeck

public function onUploadGit(){

    $id = post()['id'];
    
    $attr = Named::find($id);
    
    $signup_id = $attr['signup_widget_id'];
    
    $signup_widget = Client::find($signup_id);
    
    $token = $signup_widget['token'];
    
    $content = file_get_contents(base_path('\plugins\bm\brand\clientPortalFiles\index.html'));
    
    $file = str_replace('<REPLACE_TOKEN>', $token, $content);
    //dd($file);
    //$file = file_put_contents(base_path('\plugins\bm\brand\clientPortalFiles\index.html'), $content);
    
    //$file = "plugins/bm/brand/clientPortalFiles/index.html";
    
    $name = $attr['brand'];
    
    $data = array(
        'sha' => file_get_contents(base_path('\plugins\bm\brand\clientPortalFiles\sha.txt')),
        'message' => 'New File',
        'content' => base64_encode($file),
        'committer' => array(
            'name' => 'laqsh11',
            'email' => '[email protected]',
        )
    );
    
    $data_string = json_encode($data);
    
    $url = "https://api.github.com/repos/laqsh11/demo/contents/'$name'/index.html";
    
    $ch = curl_init($url);
    
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $authorization = "Authorization: Bearer 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";
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'User-Agent: Awesome-Octocat-App',
        'Authorization: Bearer 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'
    ));
    
    $result = curl_exec($ch);
    
}
Sinnbeck's avatar

@laksh so you didn't change it? Look at my previous post for the fix

'content' => base64_encode($file), //sends number of bytes, not content 
laksh's avatar
Level 1

@Sinnbeck sorry, i couldn't understand as you see i changed the name of str_replace from $content to $file and i don't want to use file_put_contents because it saves the token in the file and i don't want to save it i just want to replace and upload it to git

laksh's avatar
Level 1

and if i dd() file content then in preview it gives me the file code which got the updated token

laksh's avatar
Level 1

@Sinnbeck that's the error which i got after that

Request Error:Failed to connect to api.github.com port 443: Timed out{"X_OCTOBER_ASSETS":{"js":
Sinnbeck's avatar

@laksh ok. Seems that you need to spend some time on Google to find out why github api gives timeout with xampp. I don't own a windows pc so I cannot test myself

laksh's avatar
Level 1

@Sinnbeck i got the error it gives that error because it is verying https connection so i added that CURLOPT_SSL_VERIFYHOST in the curl request now that error is resolved but the file is not uploading and it gives me some html form in response

laksh's avatar
Level 1

@Sinnbeck not at all but i think it says that Invalid Request according to me there is an error of file in curl request

Sinnbeck's avatar

@laksh I'm not sure what you expect me to do? But invalid request at least gives you something to work with

laksh's avatar
Level 1

@Sinnbeck it says the file name does not match i don't know why this error will pop

Sinnbeck's avatar

@laksh most likely as don't have a directory with ' in the name?

Removed

$url = "https://api.github.com/repos/laqsh11/demo/contents/$name/index.html";

Please or to participate in this conversation.