Forum Moderators: coopster

Message Too Old, No Replies

help reading file contents and copying those contents to another file

reading, copying file contents

         

rodriguez1804

8:57 pm on Jul 25, 2008 (gmt 0)

10+ Year Member



Hi All!

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!

rodriguez1804

10:08 pm on Jul 25, 2008 (gmt 0)

10+ Year Member



problem solved. Post back later.

rodriguez1804

9:59 pm on Jul 26, 2008 (gmt 0)

10+ Year Member



Hey All!

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!");
}
}

eelixduppy

10:57 pm on Jul 26, 2008 (gmt 0)



Thanks for sharing your solution. :)

rodriguez1804

11:10 pm on Jul 26, 2008 (gmt 0)

10+ Year Member



no problem