2017-12-07 4 views
1

Ich möchte einen PHP-Code in andere Datei nach dem <?php Tag in ihm ... mit Dateihandhabung hinzufügen.Dateibehandlung in PHP

mein Code:

$myFile = "process.php"; 
$context = stream_context_create(); 
    $fp = fopen($myFile, 'r', 1, $context); 
    $data="include('connection.php');"; 
    $tmpname = md5($data); 


    file_put_contents($tmpname, $data); 
    file_put_contents($tmpname, $fp, FILE_APPEND); 
    fclose($fp); 
    unlink($myFile); 
    rename($tmpname, $myFile); 
+0

wirklich nicht brauchen, um eine neue Datei mit 'include (...) zu erstellen beantwortet' Inhalt. Was müssen Sie wirklich erreichen? – panther

+0

'$ myFile =" process.php ";
'hier ist ein Fehler –

Antwort

0

Wie wäre es

<?php 

$myFile = "process.php"; 
$gf = file($myFile); 
$data="include('connection.php');"; 
foreach($gf as $lineNumber => &$lineContent) { 
     if($lineNumber == 0) { 
       $lineContent .= $data; 
     } 
} 

$content = implode("", $gf); //Put the array back into one string 
file_put_contents($myFile, $content); 
?> 

Es Here