0

kindly help me. my env as follow : window 7 x64,wamp x64 v2.5.

apache web root directory : D:/wampProject/.

I want to write to the target text file in D:/wampProject/chapter02/orders.txt

syntaxTest.php in D:/wampProject/,as below.

<?php
header("Content-type: text/html; charset=utf-8");
$document_root= $_SERVER['DOCUMENT_ROOT'];

//#1 $filename = "$document_root"."chapter02/orders.txt"; //works
//#2 $filename="D:/wampProject/"."chapter02/orders.txt";  //works
//#3 $filename="$document_root../orders.txt"; 
  //#3 output: filename D:/wampProject/../orders.txt 
  //showed contents added in the page after refresh,
  //but text file didn't get written refer pics below . 
  //Got the wrong dir? seems like page result shows 'cache' written content 
  //added by absolute path in all time by the script.
//#4 $filename="D:/wampProject/"."../orders.txt";
   //filenameD:/wampProject/../orders.txt  as failed as #3.
if (is_writable($filename)) { 
echo 'The file is writable'; 
} else { 
echo 'The file is not writable'; 
} ; 
echo '<br/>';

if (is_readable($filename)) { 
echo 'The file is readable'; 
} else { 
echo 'The file is not readable'; 
};
$fp=fopen($filename,'ab'); 
$outputstring="address string"."\n";
echo $outputstring.'<br />';
flock($fp,LOCK_EX);
fwrite($fp,$outputstring,strlen($outputstring));
flock($fp,LOCK_UN);
fclose($fp);
echo "fclosed".'<br />';
$filecon=file_get_contents($filename);
echo $filecon."<br />";
?>

page result

page result

orders.txt after using 4 filename paths above

orders.txt after using 4 filename paths above

alessandrio
  • 4,282
  • 2
  • 29
  • 40
Q.Chen
  • 1
  • relative path only contain 'chapter02/orders.txt' this path only – Amit Gaud Jun 01 '17 at 12:26
  • Thank you! Avril. I figure it out now. #3 "$document_root..orders.txt" or #4 "D:/wampProject/"."../orders.txt" direct to the parent directory of the apache web root(D:/wampProject/).Thus the orders.txt in D:/orders.txt was written by both paths. PS:I'm an amateur. – Q.Chen Jun 01 '17 at 15:24
  • Virtual Hosts : https://stackoverflow.com/questions/23665064/project-links-do-not-work-on-wamp-server/23990618#23990618 – RiggsFolly Jun 01 '17 at 15:49
  • @RiggsFolly thank you so much! I am really joyful of embracing kindness of community. – Q.Chen Jun 01 '17 at 17:31

0 Answers0