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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kamill
    New Member
    • Dec 2006
    • 71

    #1

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

    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="multip art/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($filen ame));
    fclose($handle) ;

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

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

    // 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($filename 2, 'a')) {
    echo "Cannot open file ($filename2)";
    exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle 2, $somecontent) === FALSE) {
    echo "Cannot write to file ($filename2)";
    exit;
    }
    ?>
    <Table align="center" border="1" bgcolor="#FFFFF F">
    <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]
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    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:

    Comment

    • kamill
      New Member
      • Dec 2006
      • 71

      #3
      Thanks Ronalds... i have got the point.

      Comment

      Working...