简体   繁体   中英

How can I generate new file reading from a php file

I have a php file. I have written array items into tags in index.php file:

<?php 
  include 'arlequin_parser.php';
  $arlequin=new arlequin_parser();
  $dizi=$arlequin->arlequin_parser();
?>
[Profile]
  Title = "<?= $dizi['profiles']['Title'] ?>"
  NbSamples = <?= $dizi['profiles']['NbSamples'] ?>
  DataType = <?= $dizi['profiles']['DataType'] ?>
  GenotypicData = <?= $dizi['profiles']['GenotypicData'] ?>
  LocusSeparator = <?= $dizi['profiles']['LocusSeparator'] ?>
  MissingData = "<?= $dizi['profiles']['MissingData'] ?>"
  GameticPhase = <?= $dizi['profiles']['GameticPhase'] ?>
  RecessiveData = <?= $dizi['profiles']['RecessiveData'] ?>


[Data]
<?php foreach ($dizi['Data'] as $key=>$value): ?>
  [[Samples]]
    SampleName = "<?= $value['SampleName'] ?>"
    SampleSize = <?= $value['SampleSize'] ?>
    SampleData = {
        <?php foreach ($value['SampleData'] as $k=>$v): ?>
    <?= $v['individual'] ?>  <?= $v['repetition'] ?>    <?= $v['data']['dataString1'] ?>
            <?= $v['data']['dataString2'] ?>
    <?php endforeach; ?>
    }

  <?php endforeach; ?>

And I want to a new text file(newtext.txt) with datas reading from index.php file.How can I generate a new file clicking a button or a href link. newtext.txt should be:

[Profile]
  Title = "Hello World"
  NbSamples = 52
  DataType = MICROSAT
  GenotypicData = 1
  LocusSeparator = WHITESPACE
  MissingData = "?"
  GameticPhase = 0
  RecessiveData = 0


[Data]
  [[Samples]]
    SampleName = "pop_82"
    SampleSize = 24
    SampleData = {
    696 6969 669
    598 6965 658
    }

I try following code but code is not working:

$output = file_get_contents('index.php');

$file = fopen('new_text.txt', 'w');
fwrite($file, $output);
fclose($file);

Basic PHP IO API

$handle = fopen('newtext.txt', 'a+');
....
fwrite($handle, '[Profile]' . PHP_EOL);
$profileArray = $dizi['profile'];
foreach($profileArray as $key => $value)
{
    $isString = is_string($value);
     fwrite($handle, $key. ' = ' . ($isString?'"' : '') . $value . ($isString?'"':'') . PHP_EOL); // PHP_EOL inserts new line 
}
...
fflush($handle);
fclose($handle);

I find a solution if "index.php" file in same directory like following codes:

$output = file_get_contents('http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']).'/index.php');
$file = fopen('new_text.txt', 'w');
fwrite($file, $output);
fclose($file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM