2012-10-31 3 views
2

텍스트 파일에 변수를 쓰는 스크립트가 있습니다. fwrite()를 사용하고 있습니다. 5626_Report :PHP에서 fwrite()를 사용하여 여러 변수를 작성하는 방법은 무엇입니까?

1.As 내가 예를 들어 BATCH_ID를 사용하여 접두사와 각 보고서의 텍스트 파일 이름을 지정할 BATCH_ID에 의해 보고서가 나타납니다 내가 여기에이 문제가

<?php 

error_reporting(E_ALL^E_NOTICE); 

$filename = 'Report.txt'; 


$batch_id = $_GET['batch_id']; 
$status = $_GET['status']; 
$phone_number = $_GET['phone_number']; 

//check to see if I could open the report.txt 
    if (!$handle = fopen($filename, 'a')) { 
     echo "Cannot open file ($filename)"; 
     exit; 
    } 

    // I am trying to write each variables to the text file 
    if (fwrite($handle, $phone_number) === FALSE) { 
     echo "Cannot write to file ($filename)"; 
     exit; 
    } 

    echo "Success, wrote ($phone_number) to file ($filename)"; 

    fclose($handle); 


?> 

: 여기에 스크립트입니다. txt

2. fwrite() 함수에 둘 이상의 변수를 전달하려면 각 $ phone_number 옆에 $ status를 쓰고 싶습니다.

+0

. 도움을 구하는데도 부끄러운 일이 없습니다. 그러나 다른 사람들이 당신을 위해 당신의 문제를 해결하려고 노력하지도 않는 것에 부끄러움이 있습니다. 나는 네가 여기서 그 일을했다고 생각하지 않는다. 그러므로 네 질문에 투표 할 이유가 없다. – ATaylor

답변

3

시도해보십시오.

C의 일반적인 printf와 비슷하지만 핸들을 전달해야합니다. 예를 들어 :

fprintf($handle, "%s;%s;%s", $batch_id, $status, $phone_number); 

또는, PHPs 인라인 모인 사용할 수도 있고 사용

fwrite($handle "$batch_id;$status;$phone_number"); 

정확한 문제를 얻으려면 :

$filename = $batch_id."_report.txt"; 
$handle = fopen($filename, "a"); 
fwrite($handle, "$phone_number $status"); 

도움이 될 것이다.

+0

답장을 보내 주셔서 감사합니다. 문제는 절반이 내 문제를 해결했지만 이름이 _report 인 경우 접두사가 –

+0

이 아니므로 $ batch_id가 실제로 설정되어 있는지 확인해야합니다. $ batch_id = (isset ($ _ GET [ 'batch_id'])? $ _GET [ 'batch_id'] : "12345"; 'batch_id가 설정되어 있지 않으면 report.txt 앞에 기본값'12345 '가 붙습니다. . – ATaylor

0

당신은 훨씬 간단하게 수행 할 수 있습니다 뭔가를 모르고있는 부끄러움이 없습니다

$status = $_GET['status']; 
$phone_number = $_GET['phone_number']; 

$file = $_GET['batch_id']."_Report.txt"; 
//place here whatever you need 
$content = "Some content".$status."\n"; 
file_put_contents($file, $current); 
관련 문제