2017-10-02 1 views
0

.php 파일이 있고 원래 파일을 변경하지 않고 새로운 코드 행을 삽입하려고한다고 가정 해 봅니다.코드를 file_put_contents로 .php 파일에 삽입

예 : 나는 file_put_contents에 코드를 삽입 할 라인 3과 4 사이

require_once 'includes/application_top.php'; 
$smarty = new Smarty; 
require DIR_FS_CATALOG . 'templates/' . CURRENT_TEMPLATE . '/source/boxes.php'; 
include DIR_WS_MODULES . 'default.php'; 
require DIR_WS_INCLUDES . 'header.php'; 
$smarty->assign('language', $_SESSION['language']); 

. file_get_contents으로 파일을 읽고 코드를 삽입 할 수 있습니까?

+0

은() 문을 포함하십시오이 유용 바랍니다. – Blaise

+0

나는 당신이하려는 것을 이해하지 못합니다. 'file_put_contents'를 사용하면 원래 파일을 수정할 것입니다. – Barmar

+1

내가 원하는 것을 성취 할 수있는 더 나은 방법이있을 것입니다. 조건부 논리 또는 뭔가를 사용할 수 있습니까? – dan08

답변

0

내가 제대로 질문을 이해하면 외부 파일에서 PHP 코드를 가지고 있고 그 바로 다음과 같은 코드를 사용할 수있는 경우는, 다른 PHP 코드를 사용하여 콘텐츠에 새로운 라인을 추가 할 :

<?php 
 

 
$content = file_get_content("a.php"); 
 

 
function add_new_line($content, $insert_code, $line_number){ 
 
\t $lines = explode("\n", $content); 
 
\t $segment_1 = array_slice($lines, 0, $line_number); 
 
\t $segment_2 = array_slice($lines, $line_number, count($lines) - 1); 
 
\t array_push($segment_1, $insert_code); 
 
\t $lines = array_merge($segment_1, $segment_2); 
 
\t return $str = implode("\n", $lines); 
 
} 
 

 

 
$line_number = 3; 
 
$insert_code = "echo 'this is new line';"; 
 

 
// add new line of code to the content 
 
$new_content = add_new_line($content, $insert_code, $line_number); 
 

 
// insert new file content: 
 
file_put_content("a.php", $new_content);

난 당신이

+0

파일에 대해 정확히 한 번 실행되는 일종의 작업이면 의미가 있습니다. 사용자가 비공개 URL을 사용하는 대신 사용자가 페이지에 액세스 할 때이 코드가 실행되면 a.php는 반복적으로 추가 된 코드를 추가하게됩니다. –

관련 문제