2016-04-07 23 views
-1

Bir php dosyanız var. Ben index.php dosyasında etiketlere dizi öğeleri yazdım:Bir php dosyasından nasıl yeni dosya okuyabilirim?

<?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; ?> 

Ve yeni bir dosya tıklayarak oluşturmak yapabilirim file.How index.php okuma dataları ile yeni bir metin dosyası (newtext.txt) istiyorum Bir düğme veya bir href bağlantısı. newtext.txt olmalıdır: kod ama kod aşağıdaki deneyin

[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 
    } 

çalışmıyor:

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

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

cevap

0

Temel 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); 
+0

Nasıl yapabilirim? 'index' php dosya içeriği yerine 'Your Text' yerine –

+0

güncellendi bir örnek ile cevap – Nadir

+0

Bu kodu deneyin ama çalışmıyor: –

0

ben "index.php eğer bir çözüm bulmak "aşağıdaki dizinde olduğu gibi aynı dizinde dosya:

$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); 
İlgili konular