By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,806 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,806 IT Pros & Developers. It's quick & easy.

i need to write content of one text file into another text file.

kamill
P: 71
i need to write content of one text file into another text file.
My code is working ,if i choose both files from same directory where my program reside..BUT,its not working if i select files from out of that directory(where my application reside).

how can i overcome to it.

my code is this..

Form to select two files

[HTML]<form method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="post" value="posted" >
Select frist file(to read)<input type="file" name="txt1">
Select second file(to write)<input type="file" name="txt2">
<input type="submit" value="Submit">
</form>
[/HTML]



[PHP]if(isset($_POST['post'])=="posted")
{
echo $txt1=$_FILES['txt1']['name'];
echo $txt2=$_FILES['txt2']['name'];
// get contents of a file into a string
$filename = $txt1;
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

?>
<?php
$filename2 = $txt2;
//$filename = $contents;
$somecontent = $contents;

// Let's make sure the file exists and is writable first.
if (is_writable($filename2)) {

// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle2 = fopen($filename2, 'a')) {
echo "Cannot open file ($filename2)";
exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle2, $somecontent) === FALSE) {
echo "Cannot write to file ($filename2)";
exit;
}
?>
<Table align="center" border="1" bgcolor="#FFFFFF">
<TR><TD><?php

echo "Success, wrote ($somecontent) to file ($filename2)";
?></TD></TR></Table>
<?php
fclose($handle2);

} else {
echo "The file $filename is not writable";

}
}
?> [/PHP]
Jan 9 '07 #1
Share this Question
Share on Google+
2 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Are you UPloading the file to the server before you copy it ? If not, and it is just a local affair, why do you use the $_FILES array? And what do you expect to find in it?

If the copy is local, it is just a matter of passing the 2 file names to the copy routine via the normal $_POST and use these in your copy part of the script.

Ronald :cool:
Jan 9 '07 #2

kamill
P: 71
Thanks Ronalds... i have got the point.
Jan 9 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.