ZeroSuf3r
ZeroSuf3r

Reputation: 2001

Copy file content to another file

My file contains this content (1.txt):

(XXXXXX = numbers)

It's possible to take only user.php?id=XXXXXX and copy them to another file, without coping unnecessary text because file contains about 50 000 lines ?

Upvotes: 0

Views: 22143

Answers (4)

user7585749
user7585749

Reputation:

suppose you want to copy the contents of "abc.php" file to "working_file.php" place the following code in working_file.php , it will simply copy all the content.

 <div>
       <?php
        $file = fopen("abc.php", "r") or die("Unable to open file!");
        echo fread($myfile,filesize("abc.php"));
        fclose($file); 
        ?>
</div>

Upvotes: -1

Ingmar Boddington
Ingmar Boddington

Reputation: 3500

<?php
    //Get all the matches from the file
    $fileContents = file_get_contents('1.txt');
    preg_match_all('/user.php\?id=[0-9]{6}/', $fileContents, $matches);

    //Output to new file
    $fh = fopen('output.txt', 'w+');
    foreach ($matches['0'] as $match) {
        fputs($fh, $match."\r\n");
    }
    fclose($fh);
?>

Upvotes: 4

<?php   
error_reporting(0);
$fs=fopen("1.txt", "r");
$ft=fopen("2.txt", "w");

if ($fs==NULL)
{
    echo "Can't Open Source File ...";
    exit(0);
}

if ($ft==NULL)
{
    echo "Can't Open Destination File ...";
    fclose ($fs);
    exit(1);
}

else
{
    while ($ch=fgets($fs))
        fputs($ft, $ch);

    fclose ($fs);
    fclose ($ft);
}

//echo "File Handling successfully ...";
?>

Upvotes: -1

Siyuan Miao
Siyuan Miao

Reputation: 754

 <?php
    $file = fopen('source.txt', 'rb');
    $newfile = fopen('target.txt', 'wb');
    while(($line = fgets($file)) !== false) {
     if(strpos($line, 'user.php') !== false) {
       fputs($newfile, $line);
     }
    }
    fclose($newfile);
    fclose($file);
  ?>

This code hasn't been tested, but I think it will work properly.

Upvotes: 7

Related Questions