|
 |
|
php สอบถามเกี่ยวกับ การใช้คำสั่ง fopen fwrite ถ้าต้องการเขียน text ต่อจากตำแหน่งของ text ล่าสุดโดยที่ข้อมูล text เก่ายังอยู่ |
|
 |
|
|
 |
 |
|
fopen('.....txt','a');
a = append
|
 |
 |
 |
 |
Date :
2014-08-23 11:28:11 |
By :
cowboycnx |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$strFileName = "thaicreate.txt";
$objFopen = fopen($strFileName, 'w');
$strText1 = "I Love ThaiCreate.Com Line1\r\n";
fwrite($objFopen, $strText1);
$strText2 = "I Love ThaiCreate.Com Line2\r\n";
fwrite($objFopen, $strText2);
$strText3 = "I Love ThaiCreate.Com Line3\r\n";
fwrite($objFopen, $strText3);
mode
- r อ่านอย่างเดียว
- w สร้างไฟล์โดยถ้ามีไฟล์เดิมอยู่แล้วจะทำการลบทิ้งและสร้างไฟล์ขึ้นมาใหม่
- a กรณีที่มีไฟล์อยู่แล้วจะทำการเขียนไฟล์ต่อจากที่มีอยู่
|
 |
 |
 |
 |
Date :
2014-08-23 11:35:00 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ ถ้าเรากำหนดให้
เป็นประเภท
<?xml version="1.0" encoding="UTF-8"?>
Code (PHP)
<?php
$filename ="data.xml";
$fn = fopen ( "data.txt", "r" );
while ( ! feof ( $fn ) ) {
$current = trim ( fgets ( $fn ) );
$iArray [] = explode ( "*", $current );
}
$count = count ( $iArray );
for($x = 0; $x < $count; $x ++) {
$newArray [$x] ["title"] = trim ( $iArray [$x] [0] );
$newArray [$x] ["old_price"] = trim ( $iArray [$x] [1] );
$newArray [$x] ["price"] = trim ( $iArray [$x] [2] );
$newArray [$x] ["review"] = trim ( $iArray [$x] [3] );
$newArray [$x] ["description"] = trim ( $iArray [$x] [4] );
$newArray [$x] ["image"] = trim ( $iArray [$x] [5] );
$newArray [$x] ["link"] = trim ( $iArray [$x] [6] );
$newArray [$x] ["merchant"] = trim ( $iArray [$x] [7] );
}
$xml = "<?xml version=\"1.0\"?>\n";
$xml .= "<item>\n";
foreach ( $newArray as $row ) {
$xml .= " <product>\n";
$xml .= " <name>" . $row ['title'] . "</name>\n";
$xml .= " </product>\n";
}
$xml .= "</item>\n";
if (! $handle = fopen ( $filename, 'a' )) {
echo "Cannot open file ($filename)";
exit ();
}
if (fwrite ( $handle, $xml . "\n" ) === FALSE) {
echo "Cannot write to file ($filename)";
exit ();
}
fclose ( $handle );
?>
ช่วยดู code ให้หน่อยครับ ถ้าเราจะกำหนดให้ จากตัวอย่างเป็นแบบนี้ครับ
Code (XML)
<?xml version="1.0" encoding="UTF-8"?>
<item>
<product>
<name>Add this to the file</name>
</product>
</item>
ต้องทำยังไงครับ ถึงข้อมูลมาจาก text file
|
 |
 |
 |
 |
Date :
2014-08-23 11:51:23 |
By :
keyword2015 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เราจะกำหนดให้มันเป็น <?xml version="1.0" encoding="UTF-8"?> ด้านบน
ส่วนข้อมูลได้มากจากดึงของ data.txt ต้องเขียนแบบไหนครับ
|
 |
 |
 |
 |
Date :
2014-08-23 11:54:14 |
By :
keyword2015 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|