Forum Moderators: coopster
I am trying to write a script that gets the contents of a .tpl file and then creates a .php file with those same contents. I am currently just testing the script, but I can't seem to make it read the file contents. I am fairly new to file reading/writing using php, so any help would be great!
Here's the code:
//specify path to file (the script and .tpl are in same folder)
$uri = "index.tpl";//put all the contents in the 'contents' variable
$contents = file_get_contents($uri) or die("could not read file contents");
//file to be created
$myfile = ("index.php") or die("could not create file");
//open file handle
$fh = fopen($myfile, 'w') or die("can't open the file");
//write the file contents into the new file
fwrite($fh, $contents);
//close the file handle
fclose($fh);
//move the file to the new location (same folder)
$newFile=("index.php") or die("could not create user file");
When I run the script, it doesn't even read the file contents. why? Thanks for any help!
In response to my own question, here is the final script that I implemented with the help of various sources, just in case anybody has any questions on copying files later on:
function copyFiles($file, $newFile){
//$file is the source file PATH; $newFile is the new file name path//check to see if the file exists, if it doesn't, quit and whine
if(file_exists($file) == false){
die("File doesn't exist!");
}
//copy the file if the file exist
$result = copy($file, $newFile);
//Let the user know the result of the file copy
if($result == true){
echo "Copy Successfull!";
}else{
die("Copy Failed!");
}
}