Ganesh Babu
Ganesh Babu

Reputation: 3670

Creating JS Files using php

I have a code like this which is created using php

<?php
$script="$(document).ready(function(){alert('hai');});";
?>

As far as I have referred in other websites, they have used fopen() and fwrite() to open a text file and write in it. I don't want txt file to be created.Can I create a script file say script.js that contains data in the $script variable using php?

Upvotes: 4

Views: 10989

Answers (2)

Charaf JRA
Charaf JRA

Reputation: 8334

This will create file called script.js and contains your JS statements:

$script="$(document).ready(function(){alert('hai');});";
$fileName="script.js";
file_put_contents($fileName, $script);

Upvotes: 8

Daniel
Daniel

Reputation: 2014

do you mean like..

file_put_contents()

https://www.php.net/manual/en/function.file-put-contents.php

<?php
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>

Otherwise im not sure what you are asking.

Upvotes: 1

Related Questions